diff options
| author | tv <tv@krebsco.de> | 2026-11-11 17:03:03 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-11 17:03:03 +0000 |
| commit | d7ebb66780b090b0ac140cad0b61daf7778fc772 (patch) | |
| tree | 1b2fbafb2207c4f75632bfb5627886bb0ae5b73a /lib | |
| parent | 84888f4e20de185e3bd3aaaa63e180dace349b92 (diff) | |
lib shell: add requireFlakes
Shell code that enables flakes for the Nix commands following it, and fails
with an error message naming the host and the Nix version found, unless Nix
supports flakes.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/default.nix | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/default.nix b/lib/default.nix index 3bbd754..989920d 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -82,6 +82,20 @@ let { else "\\${c}"; in { quote = x: if x == "" then "''" else lib.stringAsChars quoteChar x; + + # Shell code that enables flakes for the Nix commands following it, and + # fails unless Nix supports them, i.e. is version 2.4 or later. `what` + # names what needs flakes, for the error message. + requireFlakes = what: /* sh */ '' + NIX_CONFIG=$(printf '%s\n%s' "''${NIX_CONFIG-}" \ + 'extra-experimental-features = nix-command flakes') + export NIX_CONFIG + if ! nix flake --help >/dev/null 2>&1; then + >&2 printf 'error: %s needs Nix 2.4 or later on %s, found: %s\n' \ + ${lib.shell.quote what} "$(uname -n)" "$(nix --version 2>&1 || :)" + exit 1 + fi + ''; }; test = re: x: lib.isString x && lib.testString re x; |
