Home

ugit @main - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / flake.nix
- raw
  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{
  description = "Minimal git server";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    gomod2nix = {
      url = "github:nix-community/gomod2nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    tailwind-ctp = {
      url = "git+https://git.jolheiser.com/tailwind-ctp";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    tailwind-ctp-lsp = {
      url = "git+https://git.jolheiser.com/tailwind-ctp-intellisense";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = {
    self,
    nixpkgs,
    gomod2nix,
    tailwind-ctp,
    tailwind-ctp-lsp,
  } @ inputs: let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
    tailwind-ctp = inputs.tailwind-ctp.packages.${system}.default;
    tailwind-ctp-lsp = inputs.tailwind-ctp-lsp.packages.${system}.default;
    ugit = gomod2nix.legacyPackages.${system}.buildGoApplication rec {
      name = "ugitd";
      src = pkgs.nix-gitignore.gitignoreSource [] (builtins.path {
        inherit name;
        path = ./.;
      });
      pwd = ./.;
      subPackages = ["cmd/ugitd"];
      CGO_ENABLED = 0;
      flags = [
        "-trimpath"
      ];
      ldflags = [
        "-s"
        "-w"
        "-extldflags -static"
      ];
      meta = with pkgs.lib; {
        description = "Minimal git server";
        homepage = "https://git.jolheiser.com/ugit";
        maintainers = with maintainers; [jolheiser];
        mainProgram = "ugitd";
      };
    };
  in {
    packages.${system}.default = ugit;
    devShells.${system}.default = pkgs.mkShell {
      nativeBuildInputs = with pkgs; [
        go
        gopls
        gomod2nix.legacyPackages.${system}.gomod2nix
        templ
        tailwind-ctp
        tailwind-ctp-lsp
        vscode-langservers-extracted
      ];
    };
    nixosModules.default = {
      pkgs,
      lib,
      config,
      ...
    }: let
      cfg = config.services.ugit;
      yamlFormat = pkgs.formats.yaml {};
      configFile = pkgs.writeText "ugit.yaml" (builtins.readFile (yamlFormat.generate "ugit-yaml" cfg.config));
      authorizedKeysFile = pkgs.writeText "ugit_keys" (builtins.concatStringsSep "\n" cfg.authorizedKeys);
    in {
      options = let
        inherit (lib) mkEnableOption mkOption types;
      in {
        services.ugit = {
          enable = mkEnableOption "Enable ugit";

          package = mkOption {
            type = types.package;
            description = "ugit package to use";
            default = ugit;
          };

          tsAuthKey = mkOption {
            type = types.str;
            description = "Tailscale one-time auth-key";
            default = "";
          };

          repoDir = mkOption {
            type = types.str;
            description = "where ugit stores repositories";
            default = "/var/lib/ugit/repos";
          };

          authorizedKeys = mkOption {
            type = types.listOf types.str;
            description = "list of keys to use for authorized_keys";
            default = [];
          };

          authorizedKeysFile = mkOption {
            type = types.str;
            description = "path to authorized_keys file ugit uses for auth";
            default = "/var/lib/ugit/authorized_keys";
          };

          hostKeyFile = mkOption {
            type = types.str;
            description = "path to host key file (will be created if it doesn't exist)";
            default = "/var/lib/ugit/ugit_ed25519";
          };

          config = mkOption {
            type = types.attrs;
            default = {};
            description = "config.yaml contents";
          };

          user = mkOption {
            type = types.str;
            default = "ugit";
            description = "User account under which ugit runs";
          };

          group = mkOption {
            type = types.str;
            default = "ugit";
            description = "Group account under which ugit runs";
          };

          openFirewall = mkOption {
            type = types.bool;
            default = false;
          };
        };
      };
      config = lib.mkIf cfg.enable {
        users.users."${cfg.user}" = {
          home = "/var/lib/ugit";
          createHome = true;
          group = "${cfg.group}";
          isSystemUser = true;
          isNormalUser = false;
          description = "user for ugit service";
        };
        users.groups."${cfg.group}" = {};
        networking.firewall = lib.mkIf cfg.openFirewall {
          allowedTCPPorts = [8448 8449];
        };

        systemd.services.ugit = {
          enable = true;
          script = let
            authorizedKeysPath =
              if (builtins.length cfg.authorizedKeys) > 0
              then authorizedKeysFile
              else cfg.authorizedKeysFile;
            args = [
              "--config=${configFile}"
              "--repo-dir=${cfg.repoDir}"
              "--ssh.authorized-keys=${authorizedKeysPath}"
              "--ssh.host-key=${cfg.hostKeyFile}"
            ];
          in "${cfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}";
          wantedBy = ["multi-user.target"];
          after = ["network.target"];
          path = [cfg.package pkgs.git pkgs.bash];
          serviceConfig = {
            User = cfg.user;
            Group = cfg.group;
            Restart = "always";
            RestartSec = "15";
            WorkingDirectory = "/var/lib/ugit";
            Environment = ["TS_AUTHKEY=${cfg.tsAuthKey}"];
          };
        };
      };
    };
  };
}