add non working test for now

This commit is contained in:
lassulus 2025-05-27 12:37:30 +02:00
parent 81e0aadf20
commit 7bea82788d
4 changed files with 52 additions and 14 deletions

View file

@ -1,16 +1,22 @@
{ {
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
outputs = { nixpkgs, ... }: let outputs = { nixpkgs, ... }@self: let
supportedArchitectures = [ supportedArchitectures = [
"aarch64-darwin" "aarch64-darwin"
"aarch64-linux" "aarch64-linux"
"x86_64-darwin" "x86_64-darwin"
"x86_64-linux" "x86_64-linux"
]; ];
in { in rec {
packages = nixpkgs.lib.genAttrs supportedArchitectures (system: { packages = nixpkgs.lib.genAttrs supportedArchitectures (system: {
nix_templater = nixpkgs.legacyPackages.${system}.callPackage ./pkgs/nix_templater {}; nix_templater = nixpkgs.legacyPackages.${system}.callPackage ./pkgs/nix_templater {};
}); });
legacyPackages = nixpkgs.lib.genAttrs supportedArchitectures (system: import ./lib.nix { pkgs = nixpkgs.legacyPackages.${system}; }); legacyPackages = nixpkgs.lib.genAttrs supportedArchitectures (system: import ./lib.nix {
pkgs = nixpkgs.legacyPackages.${system};
nix_templater = packages.${system}.nix_templater;
});
checks = nixpkgs.lib.genAttrs supportedArchitectures (system: {
template = import ./tests/template.nix { inherit legacyPackages system nixpkgs; };
});
}; };
} }

13
lib.nix
View file

@ -1,4 +1,4 @@
{ pkgs, text_templater }: { pkgs, nix_templater }:
rec { rec {
fileContents = file: { fileContents = file: {
outPath = "<${builtins.placeholder "nix_template"}${toString file}${builtins.placeholder "nix_template"}>"; outPath = "<${builtins.placeholder "nix_template"}${toString file}${builtins.placeholder "nix_template"}>";
@ -9,7 +9,8 @@ rec {
pkgs.runCommand name { pkgs.runCommand name {
textBeforeTemplate = text; textBeforeTemplate = text;
script = '' script = ''
${text_templater}/bin/text_templater ${builtins.placeholder "out"}/template ${builtins.placeholder "nix_template"} "${outPath}" #!/bin/sh
${nix_templater}/bin/nix_templater ${builtins.placeholder "out"}/template ${builtins.placeholder "nix_template"} "${outPath}"
''; '';
passAsFile = [ "script" "textBeforeTemplate" ]; passAsFile = [ "script" "textBeforeTemplate" ];
} '' } ''
@ -18,12 +19,4 @@ rec {
cp $scriptPath $out/bin/${name} cp $scriptPath $out/bin/${name}
chmod +x $out/bin/${name} chmod +x $out/bin/${name}
''; '';
test = template_text {
name = "test";
text = ''
blablabla ${fileContents (pkgs.writeText "lol" "lol")}
'';
outPath = "./test";
};
} }

View file

@ -1,6 +1,6 @@
{ writeShellApplication, python3 }: { writeShellApplication, python3 }:
writeShellApplication { writeShellApplication {
name = "text_templater"; name = "nix_templater";
runtimeInputs = [ runtimeInputs = [
python3 python3
]; ];

39
tests/template.nix Normal file
View file

@ -0,0 +1,39 @@
{ legacyPackages, system, nixpkgs }:
let
# this file would usually be outside of the store
# but in this test it isn't, because setting it up in other ways is hard :)
secret_file = (nixpkgs.legacyPackages.${system}.writeText "secret" "secret");
in (nixpkgs.lib.nixos.runTest {
hostPkgs = nixpkgs.legacyPackages.${system};
name = "nix_templates";
nodes.machine = {config, pkgs, ...}: {
config = {
systemd.services.testservice = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
ExecStartPre = "${legacyPackages.${system}.template_text {
name = "test";
text = ''
public text
${legacyPackages.${system}.fileContents secret_file}
'';
outPath = "./test";
}}/bin/test";
ExecStart = pkgs.writeScript "test_file_got_templates" ''
#!/bin/sh
cat ./test | grep -q 'secret'
'';
};
};
};
};
testScript = ''
start_all()
print(machine.execute("uname -a"))
machine.wait_for_unit("testservice.service")
print(machine.succeed("cat /root/test | grep -q secret"))
'';
})