From cfcbf224692c5f9440dcd43fba1b6f458a1362de Mon Sep 17 00:00:00 2001 From: Sebastian Jaeckel Date: Tue, 22 Apr 2025 16:57:29 +0200 Subject: [PATCH] setting sail --- .envrc | 1 + .gitignore | 3 + README.md | 75 ++++++++++++++++ flake.lock | 59 +++++++++++++ flake.nix | 14 +++ ...rc-add-placeholder-for-FOAM_INST_DIR.patch | 34 ++++++++ pkgs/openfoam/default.nix | 87 +++++++++++++++++++ shell.nix | 31 +++++++ sims/.keep | 0 9 files changed, 304 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 pkgs/openfoam/0001-etc-bashrc-add-placeholder-for-FOAM_INST_DIR.patch create mode 100644 pkgs/openfoam/default.nix create mode 100644 shell.nix create mode 100644 sims/.keep diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..db0ba05 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +sims/* +!*/.keep +.direnv diff --git a/README.md b/README.md new file mode 100644 index 0000000..2de8f46 --- /dev/null +++ b/README.md @@ -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). diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..cef5f01 --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..a776e27 --- /dev/null +++ b/flake.nix @@ -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; }; + } + ); +} diff --git a/pkgs/openfoam/0001-etc-bashrc-add-placeholder-for-FOAM_INST_DIR.patch b/pkgs/openfoam/0001-etc-bashrc-add-placeholder-for-FOAM_INST_DIR.patch new file mode 100644 index 0000000..21ee2f2 --- /dev/null +++ b/pkgs/openfoam/0001-etc-bashrc-add-placeholder-for-FOAM_INST_DIR.patch @@ -0,0 +1,34 @@ +From 8043842ab7feceae999addf2725d44ab0f65071a Mon Sep 17 00:00:00 2001 +From: Sebastian Woetzel +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 + diff --git a/pkgs/openfoam/default.nix b/pkgs/openfoam/default.nix new file mode 100644 index 0000000..aef505c --- /dev/null +++ b/pkgs/openfoam/default.nix @@ -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 diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..8338c54 --- /dev/null +++ b/shell.nix @@ -0,0 +1,31 @@ +{ pkgs ? import { } }: +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 + ''; +} diff --git a/sims/.keep b/sims/.keep new file mode 100644 index 0000000..e69de29