diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ff7afc5 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1696419054, + "narHash": "sha256-EdR+dIKCfqL3voZUDYwcvgRDOektQB9KbhBVcE0/3Mo=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "7131f3c223a2d799568e4b278380cd9dac2b8579", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix index ed73e31..cbf71f7 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,14 @@ { description = "A very basic flake"; - outputs = { self, nixpkgs }: { + #nixpkgs for testing framework + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + + outputs = { self, nixpkgs, ... }: let + # expose systems for `x86_64-linux` and `aarch64-linux` + forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" ]; + in + { packages.x86_64-linux.tasklist = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec { pname = "tasklist"; @@ -10,26 +17,27 @@ src = ./.; }; - nixosModules.malobeo-tasklist = { config, ... }: + nixosModules.malobeo-tasklist = { config, lib, pkgs, ... }: let cfg = config.services.malobeo-tasklist; tasklist-pkg = self.packages.x86_64-linux.tasklist; + pkgs = nixpkgs.legacyPackages."x86_64-linux"; in { options = { services.malobeo-tasklist = { - enable = nixpkgs.lib.mkOption { + enable = lib.mkOption { default = false; - type = nixpkgs.types.bool; - description = nixpkgs.lib.mdDoc '' + type = lib.types.bool; + description = lib.mdDoc '' Enables tasklist ''; }; }; }; - config = nixpkgs.lib.mkIf cfg.enable { - environment.systemPackages = [ tasklist-pkg]; + config = lib.mkIf cfg.enable { + environment.systemPackages = [ tasklist-pkg ]; users.users = { malobeo-tasklist = { @@ -47,14 +55,33 @@ Restart = "on-failure"; }; + preStart = '' + mkdir -m 0770 -p "/var/lib/malobeo-tasklist" + cp ${tasklist-pkg}/forms.html /var/lib/malobeo-tasklist + chmod -R 0770 /var/lib/malobeo-tasklist + ''; + #chown "malobeo-tasklist:malobeo-tasklist" "/var/lib/malobeo-tasklist" + wantedBy = [ "default.target" ]; environment = { USER = "malobeo-tasklist"; - HOME = /var/lib/malobeo-tasklist; + HOME = "/var/lib/malobeo-tasklist"; }; }; }; }; + + checks = forAllSystems (system: let + checkArgs = { + # reference to nixpkgs for the current system + pkgs = nixpkgs.legacyPackages.${system}; + # this gives us a reference to our flake but also all flake inputs + inherit self; + }; + in { + # import our test + malobeo-tasklist = import ./test.nix checkArgs; + }); }; } diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..cf84e55 --- /dev/null +++ b/lib.nix @@ -0,0 +1,20 @@ +# tests/lib.nix +# The first argument to this function is the test module itself +test: +# These arguments are provided by `flake.nix` on import, see checkArgs +{ pkgs, self}: +let + inherit (pkgs) lib; + # this imports the nixos library that contains our testing framework + nixos-lib = import (pkgs.path + "/nixos/lib") {}; +in +(nixos-lib.runTest { + hostPkgs = pkgs; + # This speeds up the evaluation by skipping evaluating documentation (optional) + defaults.documentation.enable = lib.mkDefault false; + # This makes `self` available in the NixOS configuration of our virtual machines. + # This is useful for referencing modules or packages from your own flake + # as well as importing from other flakes. + node.specialArgs = { inherit self; }; + imports = [ test ]; +}).config.result diff --git a/test.nix b/test.nix new file mode 100644 index 0000000..d2e5db7 --- /dev/null +++ b/test.nix @@ -0,0 +1,22 @@ +# ./tests/hello-world-server.nix +(import ./lib.nix) { + name = "from-nixos"; + nodes = { + # `self` here is set by using specialArgs in `lib.nix` + node1 = { self, pkgs, ... }: { + imports = [ self.nixosModules.malobeo-tasklist ]; + + services.malobeo-tasklist.enable = true; + environment.systemPackages = [ pkgs.curl ]; + }; + }; + # This is the test code that will check if our service is running correctly: + testScript = '' + start_all() + # wait for our service to start + node1.wait_for_unit("malobeo-tasklist") + output = node1.succeed("curl localhost:8000/index.html") + # Check if our webserver returns the expected result + assert "Hello world" in output, f"'{output}' does not contain 'Hello world'" + ''; +}