summaryrefslogtreecommitdiff
path: root/flake.nix
blob: fa2148dae64091013c774581b70580f097be3cdd (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
103
104
{
  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}";

        deps = with pkgs; [
          libvirt
          pkg-config
          openssl
          gcc
          glibc
        ];
      in
        rec {
          # `nix build`
          packages.homeworld = naersk-lib.buildPackage {
            pname = "homeworld";
            root = ./.;
            nativeBuildInputs = deps;
            buildInputs = deps;
          };
          defaultPackage = packages.homeworld;

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

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

            config = lib.mkIf config.services.homeworld.enable  {
              users.groups.homeworld = {
                members = [ "homeworld" "${config.services.postgresql.superUser}" ];
              };
              users.users.homeworld = {
                createHome = true;
                isSystemUser = true;
                home = "/var/lib/homeworld";
                group = "homeworld";
              };

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

              systemd.services.homeworld-init = {
                wantedBy = [ "multi-user.target" ];
                requires = [ "postgresql.service" ];
                after = [ "postgresql.service" ];
                description = "Init for Homeworld";

                script = with pkgs; ''
                  if ! [ -e /var/lib/postgresql/.homeworld-inited ]; then
                    ${config.services.postgresql.package}/bin/createuser homeworld
                    ${config.services.postgresql.package}/bin/createdb -O homeworld homeworld
                    touch /var/lib/postgresql/.homeworld-inited
                  fi
                '';

                serviceConfig = {
                  Type = "oneshot";
                  User = "${config.services.postgresql.superUser}";
                  Group = "homeworld";
                };
              };

              services.postgresql.enable = true;
            };
          };

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