aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: ceb4ddb44f962778f5f0b322d76e716abd2588d9 (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
98
99
100
101
102
{
  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
            glibc
          ];
          packages.glitch-ng = naersk-lib.buildPackage {
            pname = "glitch-ng";
            root = ./.;
            buildInputs = deps;
            nativeBuildInputs = 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"
                "${config.services.postgresql.superUser}" ];
              };
              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 = "/var/lib/glitch-ng";
                  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/postgresql/.glitch-inited ]; then
                    ${config.services.postgresql.package}/bin/createuser glitch-ng
                    ${config.services.postgresql.package}/bin/createdb -O glitch-ng glitch
                    touch /var/lib/postgresql/.glitch-inited
                  fi
                '';
                serviceConfig = {
                  Type = "oneshot";
                  User = "${config.services.postgresql.superUser}";
                  Group = "glitch-ng";
                };
              };

              services.postgresql.enable = true;

            };
          };

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