/ module.nix
module.nix
 1  { alfredPkg }:
 2  {
 3    config,
 4    pkgs,
 5    lib,
 6    ...
 7  }:
 8  
 9  let
10    cfg = config.services.alfred;
11    username = "alfred";
12    groupname = username;
13    workingDirectory = "/var/lib/alfred";
14  in
15  {
16    options.services.alfred = with lib; {
17      enable = mkEnableOption "Alfred";
18  
19      databaseFile = mkOption {
20        type = types.path;
21        description = lib.mdDoc "The SQLite database file where data will be kept.";
22      };
23  
24      matrixDataDirectory = mkOption {
25        type = types.path;
26        description = lib.mdDoc "The folder where Matrix persistence data will be kept.";
27      };
28  
29      passwordFile = mkOption {
30        type = types.path;
31        description = lib.mdDoc "The path to a file containing the password for Alfred's Matrix user (`@laughing_willow:matrix.org`).";
32      };
33  
34      restart = mkOption {
35        type =
36          with types;
37          nullOr (enum [
38            "always"
39            "on-success"
40            "on-failure"
41            "on-abnormal"
42            "on-abort"
43            "on-watchdog"
44          ]);
45        description = lib.mdDoc "The restart strategy the underlying service should follow, as per https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html#Restart= .";
46      };
47    };
48  
49    config = lib.mkIf cfg.enable {
50      systemd.services.alfred = {
51        description = "Alfred Service";
52        wantedBy = [ "multi-user.target" ];
53        serviceConfig = {
54          ExecStart = ''
55            ${alfredPkg}/bin/alfred \
56              --database ${cfg.databaseFile} \
57              --matrix-data-directory ${cfg.matrixDataDirectory} \
58              --matrix-password-file ${cfg.passwordFile}
59          '';
60          User = username;
61          WorkingDirectory = workingDirectory;
62        }
63        // lib.attrsets.optionalAttrs (cfg.restart != null) {
64          Restart = cfg.restart;
65        };
66      };
67  
68      users.users.${username} = {
69        isSystemUser = true;
70        createHome = true;
71        description = "Alfred service user";
72        home = workingDirectory;
73        group = groupname;
74      };
75      users.groups.${groupname} = { };
76    };
77  }