aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: b559c45bdabbfbcf906133006813bce0f05aa7fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
{
  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    naersk.url = "github:nix-community/naersk";
  };

  outputs = { self, nixpkgs, flake-utils, naersk }:
    flake-utils.lib.eachDefaultSystem (
      system: let
        pkgs = nixpkgs.legacyPackages."${system}";
        naersk-lib = naersk.lib."${system}";
      in
        rec {
          # `nix build`
          deps = with pkgs; [
            pkg-config
            openssl
            gcc
          ];
          packages.glitch-ng = naersk-lib.buildPackage {
            pname = "glitch-ng";
            root = ./.;
            buildInputs = deps;
          };
          defaultPackage = packages.glitch-ng;

          # `nix run`
          apps.glitch-ng = flake-utils.lib.mkApp {
            drv = packages.glitch-ng;
          };
          defaultApp = apps.glitch-ng;

          nixosModules.glitch = { config, lib, ... }: {
            options = {
              services.glitch-ng.enable = lib.mkEnableOption "enable glitch NG";
              services.glitch-ng.environment-file-location = lib.mkOption {
                type = lib.types.path;
                default = "/var/lib/glitch-ng/.env";
                description = "The location of the environment file";
              };
            };

            config = lib.mkIf config.services.glitch-ng.enable {
              users.groups.glitch-ng = {
                members = [ "glitch-ng" ];
              };
              users.users.glitch-ng = {
                createHome = true;
                isSystemUser = true;
                home = "/var/lib/glitch-ng";
                group = "glitch-ng";
              };

              systemd.services.glitch-ng = {
                wantedBy = [ "multi-user.target" ];
                after = [ "glitch-ng-init.service" ];
                requires = [ "glitch-ng-init.service" ];
                serviceConfig = {
                  User = "glitch-ng";
                  Group = "glitch-ng";
                  Restart = "always";
                  WorkingDirectory = "${defaultPackage}";
                  ExecStart = "${defaultPackage}/bin/glitch-ng";
                  EnvironmentFile = "${config.services.glitch-ng.environment-file-location}";
                };
              };

              systemd.services.glitch-ng-init = {
                wantedBy = [ "multi-user.target" ];
                requires =  [ "postgresql.service" ];
                after =  [ "postgresql.service" ];
                description = "Initialize for glitch-ng";

                script = with pkgs; ''
                  if ! [ -e /var/lib/glitch-ng/.db-created ]; then
                    runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/createuser glitch-ng
                    runuser -u ${config.services.postgresql.superUser} -- ${config.services.postgresql.package}/bin/createdb -O glitch-ng glitch
                    touch /var/lib/glitch-ng/.db-created
                  fi
                '';
                serviceConfig = {
                  Type = "oneshot";
                };
              };

              services.postgresql.enable = true;

            };
          };

          # `nix develop`
          devShell = pkgs.mkShell {
            nativeBuildInputs = with pkgs; [ rustc cargo ] ++ deps;
          };
        }
    );
}