setting sail

This commit is contained in:
Sebastian Jaeckel 2025-04-22 16:57:29 +02:00
commit cfcbf22469
Signed by: wose
GPG key ID: 6124402A3FAA8631
9 changed files with 304 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
sims/*
!*/.keep
.direnv

75
README.md Normal file
View file

@ -0,0 +1,75 @@
# OpenFOAM nix shell environment
## Usage
With `direnv` installed and active (`eval $"(direnv hook bash)"`) the
environment loads itself when you enter this directory or any below it.
Simulations can be placed inside the `sims` directory. The `sims`
directory is ignored by git. If you change branches your simulations
should stay untouched. However it may lead to confusion if you have
simulation results from one OpenFOAM version and then change to another.
Clone this repository again for each OpenFOAM version you want to use to
not confuse yourself ;-)
## Requirements
### nix with flakes
On `nixos` flakes can be enabled by adding the following to your system
configuration:
``` nix
nix.settings.experimental-features = [ "nix-command" "flakes" ];
```
On other systems add the following to your `/etc/nix/nix.conf`:
``` conf
experimental-features = nix-command flakes
```
### direnv
After installing `direnv` it has to be loaded. Maybe add the hook to your
shellrc:
``` sh
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
```
See https://direnv.net/docs/hook.html for more information.
You'll need to `allow` the current directory:
``` sh
direnv allow
```
## Update nixpkgs
From within this directory run
``` sh
nix flake update
```
to pin to the latest `nixpkgs` version.
## Update OpenFOAM version
The OpenFOAM derivation is defined in `pkgs/openfoam/default.nix`. Edit the
variables in the `let` block at the top of the file and replace the
`"sha256-..."` string with an empty string. The first build will then fail
and give you the correct sha hash. Replace the empty string with it.
## Add different OpenFOAM release
There are branches for each OpenFOAM release. The main branch has always the
latest version. If you want to add a new one:
- create a new branch to hold the current version (e.g. openfoam-12)
- update the repository path in `pkgs/openfoam/default.nix` by editing the
parameters in the `let` block.
- adjust the `patches`, `postPatch`, `configurePhase`, `buildPhase` and
`installPhase` if required
Reevaluating `envrc` will build the new OpenFOAM (this may take a while (expect
up to 1h).

59
flake.lock Normal file
View file

@ -0,0 +1,59 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1744868846,
"narHash": "sha256-5RJTdUHDmj12Qsv7XOhuospjAjATNiTMElplWnJE9Hs=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ebe4301cbd8f81c4f8d3244b3632338bbeb6d49c",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

14
flake.nix Normal file
View file

@ -0,0 +1,14 @@
{
description = "OpenFOAM 12 Simulation Environment for pirates";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem
(system:
let pkgs = nixpkgs.legacyPackages.${system}; in
{
devShells.default = import ./shell.nix { inherit pkgs; };
}
);
}

View file

@ -0,0 +1,34 @@
From 8043842ab7feceae999addf2725d44ab0f65071a Mon Sep 17 00:00:00 2001
From: Sebastian Woetzel <wose@zuendmasse.de>
Date: Sun, 20 Apr 2025 18:00:41 +0200
Subject: [PATCH] etc/bashrc: add placeholder for FOAM_INST_DIR
---
etc/bashrc | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)
diff --git a/etc/bashrc b/etc/bashrc
index 1ead5cc1c7..59d0bdb2ad 100644
--- a/etc/bashrc
+++ b/etc/bashrc
@@ -42,15 +42,8 @@ export WM_PROJECT_VERSION=12
# the directory two levels below this file, or $HOME/$WM_PROJECT if that
# directory cannot be determined.
#
-[ "$BASH" ] && bashrcFile=${BASH_SOURCE}
-[ "$ZSH_NAME" ] && bashrcFile=$0
-if [ -n "$bashrcFile" ]
-then
- export FOAM_INST_DIR=$(cd $(dirname $bashrcFile)/../.. && pwd -P)
-else
- export FOAM_INST_DIR=$HOME/$WM_PROJECT
-fi
-unset bashrcFile
+# FOAM_INST_DIR is set in postPatch phase
+# __inst_dir_placeholder__
#
# Please set to the appropriate path if the above default is not correct. E.g.,
#
--
2.49.0

87
pkgs/openfoam/default.nix Normal file
View file

@ -0,0 +1,87 @@
{
stdenv,
lib,
pkgs,
...
}: let
version = "12.20250206";
major = "12";
revision = "20250206";
realname = "OpenFOAM";
in let
pkg = stdenv.mkDerivation {
pname = "openfoam";
inherit version major;
src = pkgs.fetchFromGitHub {
owner = realname;
repo = "${realname}-${major}";
rev = "${revision}";
sha256 = "sha256-MZ29G5+49ZjE2hHC4uRF6PTR7cI5hrdjOn66lMnIgIw=";
};
nativeBuildInputs = with pkgs; [bash m4 flex bison];
buildInputs = with pkgs; [fftw mpi scotch boost cgal zlib];
patches = [
./0001-etc-bashrc-add-placeholder-for-FOAM_INST_DIR.patch
];
postPatch = ''
substituteInPlace etc/bashrc \
--replace 'export SCOTCH_TYPE=ThirdParty' 'export SCOTCH_TYPE=system' \
--replace 'export ZOLTAN_TYPE=ThirdParty' 'export ZOLTAN_TYPE=none'
patchShebangs Allwmake
patchShebangs etc
patchShebangs wmake
patchShebangs applications
patchShebangs bin
'';
configurePhase = ''
export FOAM_INST_DIR=$NIX_BUILD_TOP/source
export WM_PROJECT_DIR=$FOAM_INST_DIR/${realname}-${major}
mkdir $WM_PROJECT_DIR
mv $(find $FOAM_INST_DIR/ -maxdepth 1 -not -path $WM_PROJECT_DIR -not -path $FOAM_INST_DIR/) \
$WM_PROJECT_DIR/
set +e
. $WM_PROJECT_DIR/etc/bashrc
set -e
'';
buildPhase = ''
sh $WM_PROJECT_DIR/Allwmake -j$CORES
wclean all
wmakeLnIncludeAll
'';
installPhase = ''
mkdir -p $out/${realname}-${major}
substituteInPlace $WM_PROJECT_DIR/etc/bashrc \
--replace '# __inst_dir_placeholder__' "export FOAM_INST_DIR=$out"
cp -Ra $WM_PROJECT_DIR/* $out/${realname}-${major}
'';
passthru = {
shellHook = ''
. ${pkg}/${realname}-${major}/etc/bashrc
'';
};
meta = with pkgs.lib; {
homepage = "https://www.openfoam.org/";
description = "OpenFOAM is a free, open source CFD software released and developed by OpenFOAM Foundation";
license = licenses.gpl3;
platforms = platforms.linux;
maintainers = [ "wose" ];
broken = pkgs.stdenv.isDarwin;
};
};
in
pkg

31
shell.nix Normal file
View file

@ -0,0 +1,31 @@
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell rec {
openfoam = callPackage ./pkgs/openfoam/default.nix { };
buildInputs = [
gnumake
mpi
openfoam
];
shellHook = ''
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo "Setting sail to the open foaming sea!"
echo "Arrrrr!"
echo ""
. ${openfoam}/OpenFOAM-12/etc/bashrc
'';
}

0
sims/.keep Normal file
View file