nixpkgs/lib/tests/modules/pathWith.nix
Jeremy Fleischman 6d7f6a92cc
lib/types: add types.pathWith
This gives people some flexibility when they need a path type, and
prevents a "combinatorial explosion" of various path stops.

I've re-implemented our existing `path` and `pathInStore` types using
`pathWith`. Our existing `package` type is potentially a candidate for
similar treatment, but it's a little quirkier (there's some stuff with
`builtins.hasContext` and `toDerivation` that I don't completely
understand), and I didn't want to muddy this PR with that.

As a happy side effect of this work, we get a new feature: the ability
to create a type for paths *not* in the store. This is useful for when a
module needs a path to a file, and wants to protect people from
accidentally leaking that file into the nix store.
2025-02-15 16:14:06 +07:00

88 lines
3 KiB
Nix

{ lib, ... }:
let
inherit (builtins)
storeDir
;
inherit (lib)
types
mkOption
;
in
{
imports = [
{
options = {
pathInStore = mkOption { type = types.lazyAttrsOf (types.pathWith { inStore = true; }); };
pathNotInStore = mkOption { type = types.lazyAttrsOf (types.pathWith { inStore = false; }); };
anyPath = mkOption { type = types.lazyAttrsOf (types.pathWith { }); };
absolutePathNotInStore = mkOption {
type = types.lazyAttrsOf (
types.pathWith {
inStore = false;
absolute = true;
}
);
};
# This conflicts with `conflictingPathOptionType` below.
conflictingPathOptionType = mkOption { type = types.pathWith { absolute = true; }; };
# This doesn't make sense: the only way to have something be `inStore`
# is to have an absolute path.
impossiblePathOptionType = mkOption {
type = types.pathWith {
inStore = true;
absolute = false;
};
};
};
}
{
options = {
# This should merge cleanly with `pathNotInStore` above.
pathNotInStore = mkOption {
type = types.lazyAttrsOf (
types.pathWith {
inStore = false;
absolute = null;
}
);
};
# This conflicts with `conflictingPathOptionType` above.
conflictingPathOptionType = mkOption { type = types.pathWith { absolute = false; }; };
};
}
];
pathInStore.ok1 = "${storeDir}/0lz9p8xhf89kb1c1kk6jxrzskaiygnlh-bash-5.2-p15.drv";
pathInStore.ok2 = "${storeDir}/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15";
pathInStore.ok3 = "${storeDir}/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-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";
pathNotInStore.ok1 = "/foo/bar";
pathNotInStore.ok2 = "${storeDir}"; # strange, but consistent with `pathInStore` above
pathNotInStore.ok3 = "${storeDir}/"; # also strange, but also consistent
pathNotInStore.ok4 = "";
pathNotInStore.ok5 = "${storeDir}/.links"; # strange, but consistent with `pathInStore` above
pathNotInStore.bad1 = "${storeDir}/0lz9p8xhf89kb1c1kk6jxrzskaiygnlh-bash-5.2-p15.drv";
pathNotInStore.bad2 = "${storeDir}/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15";
pathNotInStore.bad3 = "${storeDir}/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15/bin/bash";
pathNotInStore.bad4 = ./pathWith.nix;
anyPath.ok1 = "/this/is/absolute";
anyPath.ok2 = "./this/is/relative";
anyPath.bad1 = 42;
absolutePathNotInStore.ok1 = "/this/is/absolute";
absolutePathNotInStore.bad1 = "./this/is/relative";
absolutePathNotInStore.bad2 = "${storeDir}/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15";
conflictingPathOptionType = "/foo/bar";
impossiblePathOptionType = "/foo/bar";
}