aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: da01eed37aecb81b70faaa62b19fab45bc3f87e0 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{
  description = "a makeshift CI solution";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
    pyproject-nix = {
      url = "github:nix-community/pyproject.nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = {
    self,
    nixpkgs,
    pyproject-nix,
  }: let
    system = "x86_64-linux";
    pkgs = import nixpkgs {inherit system;};
    python = pkgs.python3;
    msci = (pkgs.writeScriptBin
      "msci" (builtins.readFile ./msci)).overrideAttrs (old: {
      buildCommand = "${old.buildCommand}\n patchShebangs $out";
    });
    msci-web = pyproject-nix.lib.project.loadUVPyproject {
      projectRoot = ./.;
    };
    msci-web-attrs = msci-web.renderers.buildPythonPackage {inherit python;};
  in {
    packages."${system}" = {
      msci = pkgs.symlinkJoin {
        name = "msci";
        paths = [msci pkgs.jq pkgs.git pkgs.openssl];
        buildInputs = [pkgs.makeWrapper];
        postBuild = "wrapProgram $out/bin/msci --prefix PATH : $out/bin";
      };
      msci-web =
        python.pkgs.buildPythonPackage msci-web-attrs;
    };
    nixosModules.default = {
      lib,
      config,
      ...
    }: let
      cfg = config.makeshiftci;
    in {
      options = with lib; {
        makeshiftci = mkOption {
          type = types.submodule {
            options = {
              enable = mkEnableOption "enable makeshiftci";
              dataDir = mkOption {
                type = types.str;
                default = "/var/lib/makeshiftci";
                description = "data directory of makeshiftci";
              };
              createUser = mkEnableOption "create a non-root user";
              webUI = mkOption {
                type = types.submodule {
                  options = {
                    enable = mkEnableOption "enable makeshiftci web UI";
                    port = mkOption {
                      type = types.int;
                      default = 5000;
                      description = "port to run the web UI on";
                    };
                    timeout = mkOption {
                      type = types.int;
                      default = 3600;
                      description = "gunicorn timeout";
                    };
                    workers = mkOption {
                      type = types.int;
                      default = 4;
                      description = "number of gunicorn workers";
                    };
                  };
                };
                default = {};
              };
            };
          };
          default = {};
        };
      };
      config = lib.mkIf cfg.enable (lib.mkMerge [
        {
          environment = {
            variables.MSCI_HOME = cfg.dataDir;
            systemPackages = [self.packages."${system}".msci];
          };
          systemd.tmpfiles.settings."makeshiftci" = {
            "${cfg.dataDir}" = {
              d = {
                user =
                  if cfg.createUser
                  then "makeshiftci"
                  else "root";
                group =
                  if cfg.createUser
                  then "makeshiftci"
                  else "root";
                mode = "0750";
              };
            };
          };
          services.cron.enable = true;
          users = lib.mkIf cfg.createUser {
            users."makeshiftci" = {
              group = "makeshiftci";
              home = cfg.dataDir;
              useDefaultShell = true;
            };
          };
        }
        (lib.mkIf
          cfg.webUI.enable
          {
            systemd.services.makeshiftci-web = {
              environment = {MSCI_HOME = cfg.dataDir;};
              wantedBy = ["multi-user.target"];
              unitConfig.ConditionUser =
                if cfg.createUser
                then "makeshiftci"
                else "root";
              serviceConfig = let
                webui = pkgs.python3.withPackages (p:
                  with p; [
                    gunicorn
                    self.packages."${system}".msci-web
                  ]);
              in {
                ExecStart =
                  "${webui}/bin/gunicorn "
                  + "-w ${builtins.toString cfg.webUI.workers} "
                  + "--timeout ${builtins.toString cfg.webUI.timeout} "
                  + "-b 127.0.0.1:${builtins.toString cfg.webUI.port} "
                  + "'web:create_app()'";
              };
            };
          })
      ]);
    };
  };
}