vars: add generators.<name>.finalScript
This commit is contained in:
parent
14d3982eb5
commit
b62244d1e1
@ -1,4 +1,4 @@
|
||||
{ lib, ... }:
|
||||
{ lib, pkgs, ... }:
|
||||
let
|
||||
eval =
|
||||
module:
|
||||
@ -7,6 +7,7 @@ let
|
||||
../interface.nix
|
||||
module
|
||||
];
|
||||
specialArgs.pkgs = pkgs;
|
||||
}).config;
|
||||
|
||||
usage_simple = {
|
||||
@ -64,7 +65,43 @@ in
|
||||
config = eval { generators.imports = [ generator_module ]; };
|
||||
in
|
||||
{
|
||||
expr = lib.trace (lib.attrNames config.generators) config.generators ? my-generator;
|
||||
expr = config.generators ? my-generator;
|
||||
expected = true;
|
||||
};
|
||||
|
||||
# script can be text
|
||||
test_script_text =
|
||||
let
|
||||
config = eval {
|
||||
# imports = [ usage_simple ];
|
||||
generators.my_secret.script = ''
|
||||
echo "Hello, world!"
|
||||
'';
|
||||
};
|
||||
in
|
||||
{
|
||||
expr = config.generators.my_secret.script;
|
||||
expected = "echo \"Hello, world!\"\n";
|
||||
};
|
||||
|
||||
# script can be a derivation
|
||||
test_script_writer =
|
||||
let
|
||||
config = eval {
|
||||
# imports = [ usage_simple ];
|
||||
generators.my_secret.script = derivation {
|
||||
system = pkgs.system;
|
||||
name = "my-script";
|
||||
builder = "/bin/sh";
|
||||
args = [
|
||||
"-c"
|
||||
''touch $out''
|
||||
];
|
||||
};
|
||||
};
|
||||
in
|
||||
{
|
||||
expr = lib.hasPrefix builtins.storeDir config.generators.my_secret.script;
|
||||
expected = true;
|
||||
};
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ in
|
||||
legacyPackages.evalTests-module-clan-vars = import ./eval-tests {
|
||||
inherit lib;
|
||||
clan-core = self;
|
||||
pkgs = inputs.nixpkgs.legacyPackages.${system};
|
||||
};
|
||||
checks.module-clan-vars-eval = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } ''
|
||||
export HOME="$(realpath .)"
|
||||
|
36
nixosModules/clanCore/vars/generator.nix
Normal file
36
nixosModules/clanCore/vars/generator.nix
Normal file
@ -0,0 +1,36 @@
|
||||
{
|
||||
lib,
|
||||
config,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
{
|
||||
finalScript = lib.mkOptionDefault ''
|
||||
set -eu -o pipefail
|
||||
|
||||
export PATH="${lib.makeBinPath config.path}:${pkgs.coreutils}/bin"
|
||||
|
||||
${lib.optionalString (pkgs.stdenv.hostPlatform.isLinux) ''
|
||||
# prepare sandbox user on platforms where this is supported
|
||||
mkdir -p /etc
|
||||
|
||||
cat > /etc/group <<EOF
|
||||
root:x:0:
|
||||
nixbld:!:$(id -g):
|
||||
nogroup:x:65534:
|
||||
EOF
|
||||
|
||||
cat > /etc/passwd <<EOF
|
||||
root:x:0:0:Nix build user:/build:/noshell
|
||||
nixbld:x:$(id -u):$(id -g):Nix build user:/build:/noshell
|
||||
nobody:x:65534:65534:Nobody:/:/noshell
|
||||
EOF
|
||||
|
||||
cat > /etc/hosts <<EOF
|
||||
127.0.0.1 localhost
|
||||
::1 localhost
|
||||
EOF
|
||||
''}
|
||||
${config.script}
|
||||
'';
|
||||
}
|
@ -5,8 +5,10 @@ let
|
||||
anything
|
||||
attrsOf
|
||||
bool
|
||||
either
|
||||
enum
|
||||
listOf
|
||||
path
|
||||
str
|
||||
submoduleWith
|
||||
;
|
||||
@ -26,6 +28,12 @@ in
|
||||
};
|
||||
};
|
||||
generators = {
|
||||
default = {
|
||||
imports = [
|
||||
# default implementation of the generator
|
||||
./generator.nix
|
||||
];
|
||||
};
|
||||
type = submodule {
|
||||
freeformType = attrsOf (subOptions {
|
||||
dependencies = {
|
||||
@ -109,7 +117,17 @@ in
|
||||
- $prompts: The directory containing the prompted values as files
|
||||
The script should produce the files specified in the 'files' attribute under $out.
|
||||
'';
|
||||
type = str;
|
||||
type = either str path;
|
||||
};
|
||||
finalScript = {
|
||||
description = ''
|
||||
The final generator script, wrapped, so:
|
||||
- all required programs are in PATH
|
||||
- sandbox is set up correctly
|
||||
'';
|
||||
type = lib.types.str;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
};
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user