gitscope
← rgit / 0.1.5 / flake.nix
https://dev.kronoz.win/r/rgit.git
flake.nix · Nix
Blame Raw
  1{
  2  inputs = {
  3    nixpkgs.url = "github:NixOS/nixpkgs/staging-next";
  4
  5    crane.url = "github:ipetkov/crane";
  6    utils.url = "github:numtide/flake-utils";
  7    treefmt-nix.url = "github:numtide/treefmt-nix";
  8
  9    advisory-db = {
 10      url = "github:rustsec/advisory-db";
 11      flake = false;
 12    };
 13
 14    helix = {
 15      url = "github:JordanForks/helix";
 16      flake = false;
 17    };
 18
 19    nix-github-actions = {
 20      url = "github:nix-community/nix-github-actions";
 21      inputs.nixpkgs.follows = "nixpkgs";
 22    };
 23  };
 24
 25  outputs = { self, nixpkgs, utils, crane, advisory-db, treefmt-nix, helix, nix-github-actions }:
 26    {
 27      githubActions = nix-github-actions.lib.mkGithubMatrix {
 28        checks =
 29          builtins.mapAttrs
 30            (name: value: if name != "x86_64-linux" then removeAttrs value [ "clippy" "audit" "formatting" "doc" ] else value)
 31            { inherit (self.checks) x86_64-linux aarch64-darwin; };
 32      };
 33    }
 34    //
 35    utils.lib.eachDefaultSystem (system:
 36      let
 37        pkgs = import nixpkgs { inherit system; };
 38        craneLib = crane.mkLib pkgs;
 39        cargoOnlySrc = craneLib.cleanCargoSource ./.;
 40        src = pkgs.lib.fileset.toSource {
 41          root = ./.;
 42          fileset = pkgs.lib.fileset.unions [
 43            ./.cargo
 44            ./Cargo.toml
 45            ./Cargo.lock
 46            ./tree-sitter-grammar-repository
 47            ./src
 48            ./statics
 49            ./templates
 50            ./themes
 51            ./deny.toml
 52            ./build.rs
 53          ];
 54        };
 55        rgit-grammar = pkgs.callPackage ./grammars.nix { inherit helix; };
 56        commonArgs = {
 57          inherit src;
 58          strictDeps = true;
 59          buildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin [ pkgs.libiconv ];
 60          nativeBuildInputs = with pkgs; [ cmake clang makeBinaryWrapper ];
 61          LIBCLANG_PATH = "${pkgs.clang.cc.lib}/lib";
 62          ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib";
 63        };
 64        cargoArtifacts = craneLib.buildDepsOnly (commonArgs // { src = cargoOnlySrc; });
 65        buildArgs = commonArgs // {
 66          inherit cargoArtifacts;
 67          buildInputs = [ rgit-grammar ] ++ commonArgs.buildInputs;
 68          TREE_SITTER_GRAMMAR_LIB_DIR = rgit-grammar;
 69        };
 70        rgit = craneLib.buildPackage (buildArgs // {
 71          doCheck = false;
 72          postInstall = ''
 73            wrapProgram $out/bin/rgit \
 74              --set PATH ${pkgs.lib.makeBinPath [ pkgs.gitMinimal ]}
 75          '';
 76        });
 77        treefmt = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
 78      in
 79      {
 80        checks = {
 81          build = rgit;
 82          clippy = craneLib.cargoClippy buildArgs;
 83          doc = craneLib.cargoDoc buildArgs;
 84          audit = craneLib.cargoAudit { inherit advisory-db; src = cargoOnlySrc; };
 85          deny = craneLib.cargoDeny { inherit src; };
 86          test = craneLib.cargoNextest (buildArgs // {
 87            partitions = 1;
 88            partitionType = "count";
 89          });
 90          formatting = treefmt.config.build.check self;
 91        };
 92
 93        formatter = treefmt.config.build.wrapper;
 94
 95        packages.default = rgit;
 96        apps.default = utils.lib.mkApp { drv = rgit; };
 97
 98        devShells.default = craneLib.devShell {
 99          checks = self.checks.${system};
100        };
101
102        nixosModules.default = { config, lib, pkgs, ... }:
103          with lib;
104          let
105            cfg = config.services.rgit;
106          in
107          {
108            options.services.rgit = {
109              enable = mkEnableOption "rgit";
110              bindAddress = mkOption {
111                default = "[::]:8333";
112                description = "Address and port to listen on";
113                type = types.str;
114              };
115              dbStorePath = mkOption {
116                default = "/tmp/rgit.db";
117                description = "Path to store the temporary cache";
118                type = types.path;
119              };
120              repositoryStorePath = mkOption {
121                default = "/git";
122                description = "Path to repositories";
123                type = types.path;
124              };
125              requestTimeout = mkOption {
126                default = "10s";
127                description = "Timeout for incoming HTTP requests";
128                type = types.str;
129              };
130              package = mkOption {
131                default = rgit;
132                description = "rgit package to use";
133                type = types.package;
134              };
135            };
136
137            config = mkIf cfg.enable {
138              users.groups.rgit = { };
139              users.users.rgit = {
140                description = "RGit service user";
141                group = "rgit";
142                isSystemUser = true;
143                home = "/git";
144              };
145
146              systemd.services.rgit = {
147                enable = true;
148                wantedBy = [ "multi-user.target" ];
149                after = [ "network-online.target" ];
150                path = [ pkgs.git ];
151                serviceConfig = {
152                  Type = "exec";
153                  ExecStart = "${cfg.package}/bin/rgit --request-timeout ${cfg.requestTimeout} --db-store ${cfg.dbStorePath} ${cfg.bindAddress} ${cfg.repositoryStorePath}";
154                  Restart = "on-failure";
155
156                  User = "rgit";
157                  Group = "rgit";
158
159                  CapabilityBoundingSet = "";
160                  NoNewPrivileges = true;
161                  PrivateDevices = true;
162                  PrivateTmp = true;
163                  PrivateUsers = true;
164                  PrivateMounts = true;
165                  ProtectHome = true;
166                  ProtectClock = true;
167                  ProtectProc = "noaccess";
168                  ProcSubset = "pid";
169                  ProtectKernelLogs = true;
170                  ProtectKernelModules = true;
171                  ProtectKernelTunables = true;
172                  ProtectControlGroups = true;
173                  ProtectHostname = true;
174                  RestrictSUIDSGID = true;
175                  RestrictRealtime = true;
176                  RestrictNamespaces = true;
177                  LockPersonality = true;
178                  RemoveIPC = true;
179                  RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
180                  SystemCallFilter = [ "@system-service" "~@privileged" ];
181                };
182              };
183            };
184          };
185      });
186
187  nixConfig = {
188    extra-substituters = [ "https://rgit.cachix.org" ];
189    extra-trusted-public-keys = [ "rgit.cachix.org-1:3Wva/GHhrlhbYx+ObbEYQSYq1Yzk8x9OAvEvcYazgL0=" ];
190  };
191}