From 14d3e5685a702c6eb4777eb6ba682324bbf3a187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Mon, 12 Jun 2023 13:02:00 +0200 Subject: [PATCH 1/4] lib/types: add pathInStore --- lib/types.nix | 9 +++++++++ nixos/doc/manual/development/option-types.section.md | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/lib/types.nix b/lib/types.nix index d27d5750dfab..ddd37f260c9a 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -461,6 +461,7 @@ rec { # - strings with context, e.g. "${pkgs.foo}" or (toString pkgs.foo) # - hardcoded store path literals (/nix/store/hash-foo) or strings without context # ("/nix/store/hash-foo"). These get a context added to them using builtins.storePath. + # If you don't need a *top-level* store path, consider using pathInStore instead. package = mkOptionType { name = "package"; descriptionClass = "noun"; @@ -491,6 +492,14 @@ rec { merge = mergeEqualOption; }; + pathInStore = mkOptionType { + name = "pathInStore"; + description = "path in the Nix store"; + descriptionClass = "noun"; + check = x: isStringLike x && builtins.match "${builtins.storeDir}/[^.].*" (toString x) != null; + merge = mergeEqualOption; + }; + listOf = elemType: mkOptionType rec { name = "listOf"; description = "list of ${optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType}"; diff --git a/nixos/doc/manual/development/option-types.section.md b/nixos/doc/manual/development/option-types.section.md index 9e156ebff9d3..44bb3b4782e1 100644 --- a/nixos/doc/manual/development/option-types.section.md +++ b/nixos/doc/manual/development/option-types.section.md @@ -20,6 +20,11 @@ merging is handled. coerced to a string. Even if derivations can be considered as paths, the more specific `types.package` should be preferred. +`types.pathInStore` + +: A path that is contained in the Nix store. This can be a top-level store + path like `pkgs.hello` or a descendant like `"${pkgs.hello}/bin/hello"`. + `types.package` : A top-level store path. This can be an attribute set pointing From 0179d9f7e608b6e54fd1399689b335db360fc9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Mon, 12 Jun 2023 00:35:39 +0200 Subject: [PATCH 2/4] nixos/top-level: change extraDependencies from package to pathInStore Allows adding subdirectory flake inputs that aren't top-level store paths. --- nixos/modules/system/activation/top-level.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nixos/modules/system/activation/top-level.nix b/nixos/modules/system/activation/top-level.nix index c4427149d9c9..68eb0469d8ea 100644 --- a/nixos/modules/system/activation/top-level.nix +++ b/nixos/modules/system/activation/top-level.nix @@ -260,10 +260,10 @@ in }; system.extraDependencies = mkOption { - type = types.listOf types.package; + type = types.listOf types.pathInStore; default = []; description = lib.mdDoc '' - A list of packages that should be included in the system + A list of paths that should be included in the system closure but generally not visible to users. This option has also been used for build-time checks, but the From 18111335ed4e9bd80243987b30d8c7705a95c8e1 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 26 Jun 2023 12:50:01 +0200 Subject: [PATCH 3/4] lib/tests/modules.sh: Test types.pathInStore Add missing test cases. I think the .links case should be rejected even though it's technically a path in the store. --- lib/tests/modules.sh | 10 ++++++++++ lib/tests/modules/types.nix | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 lib/tests/modules/types.nix diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index a60228198fd7..c81febb4156f 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -63,6 +63,16 @@ checkConfigOutput '^"one two"$' config.result ./shorthand-meta.nix checkConfigOutput '^true$' config.result ./test-mergeAttrDefinitionsWithPrio.nix +# types.pathInStore +checkConfigOutput '".*/store/5lz9p8xhf89kb1c1kk6jxrzskaiygnlh-bash-5.2-p15.drv"' config.pathInStore.ok1 ./types.nix +checkConfigOutput '".*/store/xfb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15"' config.pathInStore.ok2 ./types.nix +checkConfigOutput '".*/store/xfb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15/bin/bash"' config.pathInStore.ok3 ./types.nix +checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: ""' config.pathInStore.bad1 ./types.nix +checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: ".*/store"' config.pathInStore.bad2 ./types.nix +checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: ".*/store/"' config.pathInStore.bad3 ./types.nix +checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: ".*/store/.links"' config.pathInStore.bad4 ./types.nix +checkConfigError 'A definition for option .* is not of type .path in the Nix store.. Definition values:\n\s*- In .*: "/foo/bar"' config.pathInStore.bad5 ./types.nix + # Check boolean option. checkConfigOutput '^false$' config.enable ./declare-enable.nix checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*: true' config.enable ./define-enable.nix diff --git a/lib/tests/modules/types.nix b/lib/tests/modules/types.nix new file mode 100644 index 000000000000..576db6b5b9ef --- /dev/null +++ b/lib/tests/modules/types.nix @@ -0,0 +1,24 @@ +{ lib, ... }: +let + inherit (builtins) + storeDir; + inherit (lib) + types + mkOption + ; +in +{ + options = { + pathInStore = mkOption { type = types.lazyAttrsOf types.pathInStore; }; + }; + config = { + pathInStore.ok1 = "${storeDir}/5lz9p8xhf89kb1c1kk6jxrzskaiygnlh-bash-5.2-p15.drv"; + pathInStore.ok2 = "${storeDir}/xfb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15"; + pathInStore.ok3 = "${storeDir}/xfb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15/bin/bash"; + pathInStore.bad1 = ""; + pathInStore.bad2 = "${storeDir}"; + pathInStore.bad3 = "${storeDir}/"; + pathInStore.bad4 = "${storeDir}/.links"; # technically true, but not reasonable + pathInStore.bad5 = "/foo/bar"; + }; +} From 4bdff8cbbb3ff52fc26cc74d1526617aac6ba22a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Mon, 26 Jun 2023 13:20:01 +0200 Subject: [PATCH 4/4] lib/tests: remove experimental-features Now that the lib is tested with Nix 2.3, this isn't needed any more and causes warnings. --- lib/tests/release.nix | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/tests/release.nix b/lib/tests/release.nix index 5bade7112f19..805f7a7e95d6 100644 --- a/lib/tests/release.nix +++ b/lib/tests/release.nix @@ -38,9 +38,6 @@ let export PAGER=cat cacheDir=$TEST_ROOT/binary-cache - mkdir -p $NIX_CONF_DIR - echo "experimental-features = nix-command" >> $NIX_CONF_DIR/nix.conf - nix-store --init cp -r ${../.} lib