adding nixosModule and test

This commit is contained in:
2023-10-05 16:47:24 +02:00
parent ecd76439f3
commit be58c073f0
5 changed files with 109 additions and 12 deletions

27
flake.lock generated Normal file
View File

@@ -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
}

View File

@@ -1,38 +1,50 @@
{ {
description = "A very basic flake"; description = "A very basic flake";
outputs = { self, nixpkgs }: { #nixpkgs for testing framework
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs, ... }: let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" ];
in
{
packages.x86_64-linux.tasklist = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec { packages.x86_64-linux.tasklist = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec {
pname = "tasklist"; pname = "tasklist";
version = "1.0"; version = "1.0";
vendorHash = "sha256-olHQNClxU4sykBlhlbvTL6wCUYkMxnaQdLnRwvuJffw="; vendorHash = "sha256-olHQNClxU4sykBlhlbvTL6wCUYkMxnaQdLnRwvuJffw=";
src = ./.; src = ./.;
postInstall = ''
cp forms.html $out/
'';
}; };
nixosModules.malobeo-tasklist = { config, ... }: nixosModules.malobeo-tasklist = { config, lib, pkgs, ... }:
let let
cfg = config.services.malobeo-tasklist; cfg = config.services.malobeo-tasklist;
tasklist-pkg = self.packages.x86_64-linux.tasklist; tasklist-pkg = self.packages.x86_64-linux.tasklist;
pkgs = nixpkgs.legacyPackages."x86_64-linux";
in in
{ {
options = { options = {
services.malobeo-tasklist = { services.malobeo-tasklist = {
enable = nixpkgs.lib.mkOption { enable = lib.mkOption {
default = false; default = false;
type = nixpkgs.types.bool; type = lib.types.bool;
description = nixpkgs.lib.mdDoc '' description = lib.mdDoc ''
Enables tasklist Enables tasklist
''; '';
}; };
}; };
}; };
config = nixpkgs.lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
environment.systemPackages = [ tasklist-pkg ]; environment.systemPackages = [ tasklist-pkg ];
users.users = { users = {
malobeo-tasklist = { groups.malobeo-tasklist = {};
users.malobeo-tasklist = {
description = "malobeo tasklist user"; description = "malobeo tasklist user";
group = "malobeo-tasklist"; group = "malobeo-tasklist";
isNormalUser = true; isNormalUser = true;
@@ -43,18 +55,32 @@
description = "malobeo tasklist daemon"; description = "malobeo tasklist daemon";
serviceConfig = { serviceConfig = {
Type = "simple"; Type = "simple";
ExecStart = "${tasklist-pkg}/bin/tasklist -d /var/lib/malobeo-tasklist/tasklist.db"; ExecStart = "${tasklist-pkg}/bin/tasklist -d /var/lib/malobeo-tasklist/tasklist.db -f ${tasklist-pkg}/forms.html";
Restart = "on-failure"; Restart = "on-failure";
}; };
preStart = ''
mkdir -m 0770 -p "/var/lib/malobeo-tasklist"
chown malobeo-tasklist:malobeo-tasklist "/var/lib/malobeo-tasklist"
'';
wantedBy = [ "default.target" ]; wantedBy = [ "default.target" ];
environment = { environment = {
USER = "malobeo-tasklist"; USER = "malobeo-tasklist";
HOME = /var/lib/malobeo-tasklist; HOME = "/var/lib/malobeo-tasklist";
}; };
}; };
}; };
}; };
checks = forAllSystems (system: let
checkArgs = {
pkgs = nixpkgs.legacyPackages.${system};
inherit self;
};
in {
malobeo-tasklist = import ./test/test.nix checkArgs;
});
}; };
} }

View File

@@ -195,11 +195,13 @@ func GetNextNDaysOfName(n int, name time.Weekday, current time.Time) []time.Time
} }
const DB_NAME = "./tasklist.db" const DB_NAME = "./tasklist.db"
const FORMS_NAME = "./forms.html"
const TASK_DAY = time.Tuesday const TASK_DAY = time.Tuesday
const AMOUNT_DAYS = 4 const AMOUNT_DAYS = 4
func main() { func main() {
dbName := flag.String("d", DB_NAME, "path to db file") dbName := flag.String("d", DB_NAME, "path to db file")
formsName := flag.String("f", FORMS_NAME, "path to form file")
flag.Parse() flag.Parse()
db := InitDB(*dbName) db := InitDB(*dbName)
@@ -210,7 +212,7 @@ func main() {
return return
} }
tmpl := template.Must(template.ParseFiles("forms.html")) tmpl := template.Must(template.ParseFiles(*formsName))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
days := GetNextNDaysOfName(AMOUNT_DAYS, TASK_DAY, time.Now()) days := GetNextNDaysOfName(AMOUNT_DAYS, TASK_DAY, time.Now())
tasklist_arr := QueryResult{make([]Tasklist, AMOUNT_DAYS)} tasklist_arr := QueryResult{make([]Tasklist, AMOUNT_DAYS)}

21
test/lib.nix Normal file
View File

@@ -0,0 +1,21 @@
# tests/lib.nix
# based on https://blog.thalheim.io/2023/01/08/how-to-use-nixos-testing-framework-with-flakes/
# 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

21
test/test.nix Normal file
View File

@@ -0,0 +1,21 @@
# ./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 ];
};
};
testScript = ''
start_all() # wait for our service to start
node1.wait_for_unit("malobeo-tasklist")
output = node1.succeed("curl localhost:8080")
# Check if our webserver returns the expected result
assert "tasklist" in output, f"'{output}' does not contain 'Hello world'"
'';
}