Skip to content

Commit b1eb5bc

Browse files
committed
Nix: dynamically generate packages
1 parent 306633d commit b1eb5bc

File tree

10 files changed

+310
-90
lines changed

10 files changed

+310
-90
lines changed

flake.nix

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,79 @@
11
# flake.nix
2+
# To list all variations, use the following command:
3+
# nix flake show | grep "openpfcVariations"
24

35
{
46
description = "OpenPFC and HeFFTe builder";
57

6-
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
7-
inputs.flake-utils.url = "github:numtide/flake-utils";
8+
# Inputs define external dependencies for this flake.
9+
inputs.nixpkgs.url =
10+
"github:NixOS/nixpkgs/nixos-23.11"; # Nixpkgs repository for system packages.
11+
inputs.flake-utils.url =
12+
"github:numtide/flake-utils"; # Utility library for flakes.
813

14+
# Outputs define what this flake provides.
915
outputs = { self, nixpkgs, flake-utils, ... }:
1016
flake-utils.lib.eachDefaultSystem (system:
1117
let
12-
18+
# Import the Nixpkgs package set for the current system.
1319
pkgs = import nixpkgs { inherit system; };
1420

21+
# Define paths and versions for HeFFTe.
1522
hefftePath = ./nix/heffte/default.nix;
16-
heffteVersions = builtins.fromJSON (builtins.readFile ./nix/heffte/versions.json);
23+
heffteVersions =
24+
builtins.fromJSON (builtins.readFile ./nix/heffte/versions.json);
1725
heffteVersion = heffteVersions.current;
1826

27+
# Define paths and versions for OpenPFC.
1928
openpfcPath = ./nix/openpfc/default.nix;
20-
openpfcVersions = builtins.fromJSON (builtins.readFile ./nix/openpfc/versions.json);
29+
openpfcVersions =
30+
builtins.fromJSON (builtins.readFile ./nix/openpfc/versions.json);
2131
openpfcVersion = openpfcVersions.current;
2232

23-
in
24-
25-
{
26-
27-
packages = rec {
28-
29-
heffte = pkgs.callPackage hefftePath {
30-
version = heffteVersion;
31-
src = pkgs.fetchFromGitHub {
32-
owner = "icl-utk-edu";
33-
repo = "heffte";
34-
inherit (heffteVersions.versions.${heffteVersion}) rev sha256;
33+
# Debugging: Print the versions being processed.
34+
# builtins.trace ("Processing versions: ")
35+
# + builtins.concatStringsSep ", " (builtins.attrNames openpfcVersions.versions);
36+
37+
# versionedPackages: Dynamically generates OpenPFC packages for specific versions.
38+
versionedPackages = builtins.listToAttrs (map (version: {
39+
name = "openpfc-${version}";
40+
value = pkgs.callPackage ./nix/openpfc/default.nix {
41+
version = version;
42+
buildType = if version == "dev" then "Debug" else "Release";
43+
src = if version == "dev" then
44+
./.
45+
else if version == "master" then
46+
builtins.fetchGit {
47+
url = "https://github.com/VTT-ProperTune/OpenPFC.git";
48+
ref = "master";
49+
}
50+
else
51+
pkgs.fetchFromGitHub {
52+
owner = "VTT-ProperTune";
53+
repo = "OpenPFC";
54+
inherit (openpfcVersions.versions.${version}) rev sha256;
55+
};
56+
enableTests = true;
57+
enableExamples = true;
58+
enableApps = true;
59+
enableDocs = true;
60+
heffte = pkgs.callPackage ./nix/heffte/default.nix {
61+
version = heffteVersion;
62+
src = pkgs.fetchFromGitHub {
63+
owner = "icl-utk-edu";
64+
repo = "heffte";
65+
inherit (heffteVersions.versions.${heffteVersion}) rev sha256;
66+
};
3567
};
3668
};
69+
}) ([ "dev" "master" ] ++ builtins.attrNames openpfcVersions.versions));
3770

38-
# OpenPFC versioned release
39-
openpfc = pkgs.callPackage openpfcPath {
40-
version = openpfcVersion;
41-
src = pkgs.fetchFromGitHub {
42-
owner = "VTT-ProperTune";
43-
repo = "OpenPFC";
44-
inherit (openpfcVersions.versions.${openpfcVersion}) rev sha256;
45-
};
46-
heffte = self.packages.${system}.heffte;
47-
};
48-
49-
# OpenPFC from local checkout (your dev version)
50-
openpfc-dev = pkgs.callPackage openpfcPath {
51-
version = "dev";
52-
src = ./.;
53-
heffte = self.packages.${system}.heffte;
54-
};
55-
56-
default = openpfc-dev;
57-
58-
};
71+
in {
72+
# Combine dynamically generated OpenPFC packages with their dependencies.
73+
packages = versionedPackages;
5974

75+
# Development shell configuration.
6076
devShells.default = pkgs.mkShell {
61-
6277
packages = [
6378
pkgs.cmake
6479
pkgs.git
@@ -79,8 +94,6 @@
7994
echo "👉 To configure the project, run: cmake -S . -B build"
8095
echo "👉 To build the project, run: cmake --build build"
8196
'';
82-
8397
};
84-
}
85-
);
98+
});
8699
}

nix/README.md

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,114 @@ cmake -S . -B build
4343
cmake --build build
4444
```
4545

46-
### Building Specific Releases
46+
### Building OpenPFC
4747

48-
To build a specific release of `OpenPFC` with a specific version of `HeFFTe`,
49-
use the `nix build` command. There are two main build targets:
48+
The build targets follow the pattern:
5049

51-
- `#openpfc-dev`: The default target, which can also be invoked with `nix
52-
build`. This builds the development version of `OpenPFC`.
53-
- `#openpfc`: Builds the release version of `OpenPFC`.
50+
```
51+
#openpfc-<what>-<version>
52+
```
5453

55-
For example:
54+
- `<what>` can be one of the following:
55+
- Empty: Builds the full release version.
56+
- `apps`: Builds only the apps.
57+
- `docs`: Builds only the documentation.
58+
- `tests`: Builds only the tests.
59+
- `examples`: Builds only the examples.
60+
- `<version>` specifies the version number without the `v` prefix. Special values:
61+
- `dev`: Builds from the local source directory (defaults to `Debug` build type).
62+
- `master`: Builds the bleeding-edge version from the GitHub repository (defaults to `Release` build type).
5663

57-
```bash
58-
nix build #openpfc
59-
```
64+
### Overriding the Build Type
6065

61-
or equivalently for the development version:
66+
By default:
67+
- `dev` builds use the `Debug` build type.
68+
- All other builds use the `Release` build type.
6269

70+
You can override this behavior by passing the `--arg buildType <type>` argument, where `<type>` can be `Debug` or `Release`.
71+
72+
For example:
6373
```bash
64-
nix build #openpfc-dev
74+
nix build .#openpfc --arg buildType '"Debug"'
6575
```
6676

67-
When building, tagged versions are used, which are defined in the following files:
77+
### Examples
78+
79+
Here are some examples of how to use the build targets:
80+
81+
- **Build the default release version**:
82+
```bash
83+
nix build .#openpfc
84+
```
6885

69-
- `nix/openpfc/versions`
70-
- `nix/heffte/versions`
86+
- **Build the development version from the source directory**:
87+
```bash
88+
nix build .#openpfc-dev
89+
```
7190

72-
This approach allows constructing immutable builds for all versions simply by
73-
changing the version numbers in these files.
91+
- **Build a specific version (e.g., 0.1.1)**:
92+
```bash
93+
nix build .#openpfc-0.1.1
94+
```
95+
96+
- **Build tests for a specific version (e.g., 0.1.0)**:
97+
```bash
98+
nix build .#openpfc-tests-0.1.0
99+
```
100+
101+
- **Build documentation for a specific version (e.g., 0.1.1)**:
102+
```bash
103+
nix build .#openpfc-docs-0.1.1
104+
```
105+
106+
- **Build documentation for the bleeding-edge version**:
107+
```bash
108+
nix build .#openpfc-docs-master
109+
```
110+
111+
- **Build tests for the development version**:
112+
```bash
113+
nix build .#openpfc-tests-dev
114+
```
115+
116+
- **Override the build type to Debug for a release version**:
117+
```bash
118+
nix build .#openpfc-0.1.1 --arg buildType '"Debug"'
119+
```
120+
121+
These are all the build targets available in this project so far:
122+
123+
```
124+
nix build .#openpfc
125+
nix build .#openpfc-0.1.0
126+
nix build .#openpfc-0.1.1
127+
nix build .#openpfc-master
128+
nix build .#openpfc-dev
129+
130+
nix build .#openpfc-tests
131+
nix build .#openpfc-tests-0.1.0
132+
nix build .#openpfc-tests-0.1.1
133+
nix build .#openpfc-tests-master
134+
nix build .#openpfc-tests-dev
135+
136+
nix build .#openpfc-docs
137+
nix build .#openpfc-docs-0.1.0
138+
nix build .#openpfc-docs-0.1.1
139+
nix build .#openpfc-docs-master
140+
nix build .#openpfc-docs-dev
141+
142+
nix build .#openpfc-apps
143+
nix build .#openpfc-apps-0.1.0
144+
nix build .#openpfc-apps-0.1.1
145+
nix build .#openpfc-apps-master
146+
nix build .#openpfc-apps-dev
147+
148+
nix build .#openpfc-examples
149+
nix build .#openpfc-examples-0.1.0
150+
nix build .#openpfc-examples-0.1.1
151+
nix build .#openpfc-examples-master
152+
nix build .#openpfc-examples-dev
153+
```
74154

75155
### Why Use Nix?
76156

nix/heffte/versions.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"current": "v2.4.1",
2+
"current": "2.4.1",
33
"versions": {
4-
"v2.3.0": {
4+
"2.3.0": {
55
"rev": "v2.3.0",
66
"sha256": ""
77
},
8-
"v2.4.1": {
8+
"2.4.1": {
99
"rev": "v2.4.1",
1010
"sha256": "3qCF3nsxjjj3OOks8f4Uu1L3+budRPX1i+iwXy8hLhE="
1111
}

nix/openpfc/apps.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{ openpfcVariations }:
2+
3+
builtins.listToAttrs (map (version: {
4+
name = "openpfc-apps-${version}";
5+
value = openpfcVariations.dynamicVariation {
6+
version = version;
7+
enableTests = false;
8+
enableExamples = false;
9+
enableApps = true;
10+
enableDocs = false;
11+
};
12+
}) (builtins.attrNames openpfcVariations.self.inputs.openpfcVersions.versions))

nix/openpfc/default.nix

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
# nix/openpfc/default.nix
22

3-
{ lib
4-
, stdenv
5-
, cmake
6-
, mpi
7-
, heffte
8-
, nlohmann_json
9-
, catch2_3 ? null
10-
, doxygen ? null
11-
, enableDocs ? false
12-
, enableTests ? true
13-
, enableExamples ? true
14-
, enableApps ? true
15-
, fetchFromGitHub
16-
, version
17-
, src
18-
}:
3+
{ lib, stdenv, cmake, mpi, heffte, nlohmann_json, catch2_3 ? null
4+
, doxygen ? null, version, src, buildType ? "Release", enableDocs ? true
5+
, enableTests ? true, enableExamples ? true, enableApps ? true }:
196

207
stdenv.mkDerivation {
218
pname = "openpfc";
@@ -29,17 +16,15 @@ stdenv.mkDerivation {
2916

3017
nativeBuildInputs = [ cmake ];
3118

32-
buildInputs = [ mpi heffte nlohmann_json ]
33-
++ lib.optional enableDocs doxygen
19+
buildInputs = [ mpi heffte nlohmann_json ] ++ lib.optional enableDocs doxygen
3420
++ lib.optional enableTests catch2_3;
3521

3622
cmakeFlags = [
37-
"-DCMAKE_BUILD_TYPE=Release"
23+
"-DCMAKE_BUILD_TYPE=${buildType}"
3824
"-DOpenPFC_BUILD_TESTS=${if enableTests then "ON" else "OFF"}"
3925
"-DOpenPFC_BUILD_EXAMPLES=${if enableExamples then "ON" else "OFF"}"
4026
"-DOpenPFC_BUILD_APPS=${if enableApps then "ON" else "OFF"}"
4127
"-DOpenPFC_BUILD_DOCUMENTATION=${if enableDocs then "ON" else "OFF"}"
4228
"-DHeffte_DIR=${heffte}/lib/cmake/Heffte"
4329
];
44-
4530
}

nix/openpfc/docs.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{ openpfcVariations, openpfcVersions }:
2+
3+
builtins.listToAttrs (map (version: {
4+
name = "openpfc-docs-${version}";
5+
value = openpfcVariations.dynamicVariation {
6+
version = version;
7+
enableTests = false;
8+
enableExamples = false;
9+
enableApps = false;
10+
enableDocs = true;
11+
};
12+
}) (builtins.attrNames openpfcVersions.versions))

nix/openpfc/examples.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{ openpfcVariations }:
2+
3+
builtins.listToAttrs (map (version: {
4+
name = "openpfc-examples-${version}";
5+
value = openpfcVariations.dynamicVariation {
6+
version = version;
7+
enableTests = false;
8+
enableExamples = true;
9+
enableApps = false;
10+
enableDocs = false;
11+
};
12+
}) (builtins.attrNames openpfcVariations.self.inputs.openpfcVersions.versions))

nix/openpfc/tests.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{ openpfcVariations }:
2+
3+
builtins.listToAttrs (map (version: {
4+
name = "openpfc-tests-${version}";
5+
value = openpfcVariations.dynamicVariation {
6+
version = version;
7+
enableTests = true;
8+
enableExamples = false;
9+
enableApps = false;
10+
enableDocs = false;
11+
};
12+
}) (builtins.attrNames openpfcVariations.self.inputs.openpfcVersions.versions))

0 commit comments

Comments
 (0)