aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: ce7ff112ef6ff166d6eb04a69e657092ffc80ea7 (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
{
  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
        ];
      in
        rec {
          # `nix build`
          packages.solard = naersk-lib.buildPackage {
            pname = "solard";
            root = ./.;
            nativeBuildInputs = deps;
            buildInputs = deps;
          };
          defaultPackage = packages.solard;

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

          nixosModules.solard = { config, lib, ... }: {
            options = with lib; {
              cara.services.solard.enable = mkEnableOption "enable solard";
              cara.services.solard.environmentFileLocation = mkOption {
                type = types.str;
                default = "/var/lib/solard/.env";
                description = "The location of the environment file";
              };
              cara.services.solard.port = mkOption {
                type = types.port;
                default = 3000;
                description = "port to bind to";
              };
            };

            config = lib.mkIf config.cara.services.solard.enable {
              users.groups.solard = {
                members = [ "solard" ];
              };
              users.users.solard = {
                createHome = true;
                isSystemUser = true;
                home = "/var/lib/solard";
                group = "solard";
                extraGroups = [ "libvirtd" ];
              };

              systemd.services.solard = {
                wantedBy = [ "multi-user.target" ];

                serviceConfig = {
                  User = "solard";
                  Group = "solard";
                  Restart = "always";
                  WorkingDirectory = "/var/lib/solard";
                  ExecStart = "${defaultPackage}/bin/solard";
                  EnvironmentFIle = "${config.cara.services.solard.environmentFileLocation}";
                };
              };
              
              networking.firewall.allowedTCPPorts = [ config.cara.services.solard.port ];
              
            };
          };

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