|
| 1 | +# TODO remove once this is merged in nixpkgs and backported to nixos stable |
| 2 | +# Module by @dtomvan on github, MIT licensed, https://github.com/dtomvan/puntbestanden/blob/hoofdlijn/modules/services/git-pages.nix |
| 3 | +{ |
| 4 | + config, |
| 5 | + lib, |
| 6 | + pkgs, |
| 7 | + options, |
| 8 | + ... |
| 9 | +}: |
| 10 | +let |
| 11 | + cfg = config.git-pages; |
| 12 | + settingsFormat = pkgs.formats.toml { }; |
| 13 | + configFile = "git-pages.toml"; |
| 14 | + configOutPath = config.configData.${configFile}.path; |
| 15 | +in |
| 16 | +{ |
| 17 | + _class = "service"; |
| 18 | + |
| 19 | + meta.maintainers = with lib.maintainers; [ |
| 20 | + dtomvan |
| 21 | + phanirithvij |
| 22 | + ]; |
| 23 | + |
| 24 | + options.git-pages = { |
| 25 | + package = lib.mkOption { |
| 26 | + description = "Package to use for git-pages"; |
| 27 | + defaultText = "The git-pages package that provided this module."; |
| 28 | + type = lib.types.package; |
| 29 | + }; |
| 30 | + |
| 31 | + port = lib.mkOption { |
| 32 | + description = "Port to open the main pages server on"; |
| 33 | + default = 3000; |
| 34 | + type = lib.types.port; |
| 35 | + }; |
| 36 | + |
| 37 | + caddyPort = lib.mkOption { |
| 38 | + description = "Port for caddy"; |
| 39 | + default = 3001; |
| 40 | + type = lib.types.nullOr lib.types.port; |
| 41 | + }; |
| 42 | + |
| 43 | + metricsPort = lib.mkOption { |
| 44 | + description = "Port to open a prometheus exporter on"; |
| 45 | + default = 3002; |
| 46 | + type = lib.types.nullOr lib.types.port; |
| 47 | + }; |
| 48 | + |
| 49 | + secretFile = lib.mkOption { |
| 50 | + description = "Path to secrets.toml"; |
| 51 | + default = null; |
| 52 | + type = lib.types.nullOr lib.types.str; |
| 53 | + }; |
| 54 | + |
| 55 | + settings = lib.mkOption { |
| 56 | + type = settingsFormat.type; |
| 57 | + description = "Settings to set in git-pages.toml. See https://codeberg.org/git-pages/git-pages"; |
| 58 | + default = settingsFormat.emptyValue; |
| 59 | + }; |
| 60 | + }; |
| 61 | + |
| 62 | + config = { |
| 63 | + git-pages.settings.server = { |
| 64 | + pages = lib.mkOptionDefault "tcp/0.0.0.0:${toString cfg.port}"; |
| 65 | + caddy = lib.mkOptionDefault ( |
| 66 | + if cfg.caddyPort == null then "-" else "tcp/0.0.0.0:${toString cfg.caddyPort}" |
| 67 | + ); |
| 68 | + metrics = lib.mkOptionDefault ( |
| 69 | + if cfg.metricsPort == null then "-" else "tcp/0.0.0.0:${toString cfg.metricsPort}" |
| 70 | + ); |
| 71 | + }; |
| 72 | + |
| 73 | + process.argv = [ |
| 74 | + (lib.getExe cfg.package) |
| 75 | + "-config" |
| 76 | + configOutPath |
| 77 | + ]; |
| 78 | + |
| 79 | + configData."${configFile}".source = settingsFormat.generate configFile cfg.settings; |
| 80 | + } |
| 81 | + // lib.optionalAttrs (options ? systemd) { |
| 82 | + systemd.mainExecStart = config.systemd.lib.escapeSystemdExecArgs config.process.argv; |
| 83 | + |
| 84 | + systemd.service = { |
| 85 | + description = "Forge-agnostic static site server"; |
| 86 | + documentation = [ "https://codeberg.org/git-pages/git-pages" ]; |
| 87 | + |
| 88 | + after = [ "network.target" ]; |
| 89 | + wants = [ "network.target" ]; |
| 90 | + wantedBy = [ "multi-user.target" ]; |
| 91 | + restartTriggers = [ config.configData."${configFile}".source ]; |
| 92 | + |
| 93 | + serviceConfig = { |
| 94 | + ExecStartPre = "${lib.getExe' pkgs.coreutils "mkdir"} -p data"; |
| 95 | + Restart = "always"; |
| 96 | + |
| 97 | + StateDirectory = "git-pages"; |
| 98 | + WorkingDirectory = "/var/lib/git-pages"; |
| 99 | + BindReadOnlyPaths = [ configOutPath ]; |
| 100 | + |
| 101 | + LoadCredential = lib.optional (cfg.secretFile != null) "secrets.toml:${cfg.secretFile}"; |
| 102 | + |
| 103 | + # systemd service hardening |
| 104 | + DynamicUser = true; |
| 105 | + ProtectHome = true; |
| 106 | + MemoryDenyWriteExecute = true; |
| 107 | + PrivateDevices = true; |
| 108 | + PrivateTmp = true; |
| 109 | + ProtectSystem = "strict"; |
| 110 | + ProtectControlGroups = true; |
| 111 | + RestrictSUIDSGID = true; |
| 112 | + RestrictRealtime = true; |
| 113 | + LockPersonality = true; |
| 114 | + ProtectKernelLogs = true; |
| 115 | + ProtectKernelTunables = true; |
| 116 | + ProtectHostname = true; |
| 117 | + ProtectKernelModules = true; |
| 118 | + PrivateUsers = true; |
| 119 | + ProtectClock = true; |
| 120 | + SystemCallArchitectures = "native"; |
| 121 | + SystemCallErrorNumber = "EPERM"; |
| 122 | + SystemCallFilter = "@system-service"; |
| 123 | + }; |
| 124 | + }; |
| 125 | + }; |
| 126 | +} |
0 commit comments