From 7bea82788de12edf6d2f8a7ac56f6a82bd5c3d0a Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 27 May 2025 12:37:30 +0200 Subject: [PATCH] add non working test for now --- flake.nix | 12 ++++++++--- lib.nix | 13 +++--------- pkgs/nix_templater/default.nix | 2 +- tests/template.nix | 39 ++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 tests/template.nix diff --git a/flake.nix b/flake.nix index 06c7061..f5a8277 100644 --- a/flake.nix +++ b/flake.nix @@ -1,16 +1,22 @@ { inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; - outputs = { nixpkgs, ... }: let + outputs = { nixpkgs, ... }@self: let supportedArchitectures = [ "aarch64-darwin" "aarch64-linux" "x86_64-darwin" "x86_64-linux" ]; - in { + in rec { packages = nixpkgs.lib.genAttrs supportedArchitectures (system: { 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; }; + }); }; } diff --git a/lib.nix b/lib.nix index d77184e..0f34930 100644 --- a/lib.nix +++ b/lib.nix @@ -1,4 +1,4 @@ -{ pkgs, text_templater }: +{ pkgs, nix_templater }: rec { fileContents = file: { outPath = "<${builtins.placeholder "nix_template"}${toString file}${builtins.placeholder "nix_template"}>"; @@ -9,7 +9,8 @@ rec { pkgs.runCommand name { textBeforeTemplate = text; 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" ]; } '' @@ -18,12 +19,4 @@ rec { cp $scriptPath $out/bin/${name} chmod +x $out/bin/${name} ''; - - test = template_text { - name = "test"; - text = '' - blablabla ${fileContents (pkgs.writeText "lol" "lol")} - ''; - outPath = "./test"; - }; } diff --git a/pkgs/nix_templater/default.nix b/pkgs/nix_templater/default.nix index 96a577f..46bff96 100644 --- a/pkgs/nix_templater/default.nix +++ b/pkgs/nix_templater/default.nix @@ -1,6 +1,6 @@ { writeShellApplication, python3 }: writeShellApplication { - name = "text_templater"; + name = "nix_templater"; runtimeInputs = [ python3 ]; diff --git a/tests/template.nix b/tests/template.nix new file mode 100644 index 0000000..d7e92de --- /dev/null +++ b/tests/template.nix @@ -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")) + ''; + })