Skip to content

Instantly share code, notes, and snippets.

@yunfachi
Created August 16, 2024 08:55
Show Gist options
  • Select an option

  • Save yunfachi/3fc4c74f90c3b0a70ca6ccaaa775240b to your computer and use it in GitHub Desktop.

Select an option

Save yunfachi/3fc4c74f90c3b0a70ca6ccaaa775240b to your computer and use it in GitHub Desktop.
default value for read only in submodule
(import <nixpkgs/lib>).evalModules {
modules = [
({
lib,
config,
...
}: let
niceSubmodule = lib.types.submodule ({config, ...}: {
options = {
text =
(lib.mkOption {
type = lib.types.str;
readOnly = true;
})
// (
if (builtins.any (submodule: submodule ? config.text) config._module.args.moduleType.getSubModules)
then {}
else {
default = "readonly default value";
}
);
};
});
in {
options = {
original1 = lib.mkOption {type = niceSubmodule;};
copy1 = lib.mkOption {type = niceSubmodule;};
original2 = lib.mkOption {type = niceSubmodule;};
copy2 = lib.mkOption {type = niceSubmodule;};
};
config = {
original1 = {text = "orig";};
copy1 = config.original1;
original2 = {};
copy2 = config.original2;
};
})
];
}
nix eval -f ./default.nix config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment