|
| 1 | +{ lib, pkgs, config, ... }: |
| 2 | +with lib; |
| 3 | + |
| 4 | +let |
| 5 | + settingsFormat = pkgs.formats.yaml { }; |
| 6 | + |
| 7 | + # gemstash uses a yaml config where the keys are ruby symbols, |
| 8 | + # which means they start with ':'. This would be annoying to use |
| 9 | + # on the nix side, so we rewrite plain names instead. |
| 10 | + prefixColon = s: listToAttrs (map |
| 11 | + (attrName: { |
| 12 | + name = ":${attrName}"; |
| 13 | + value = |
| 14 | + if isAttrs s.${attrName} |
| 15 | + then prefixColon s."${attrName}" |
| 16 | + else s."${attrName}"; |
| 17 | + }) |
| 18 | + (attrNames s)); |
| 19 | + |
| 20 | + # parse the port number out of the tcp://ip:port bind setting string |
| 21 | + parseBindPort = bind: strings.toInt (last (strings.splitString ":" bind)); |
| 22 | + |
| 23 | + cfg = config.services.gemstash; |
| 24 | +in |
| 25 | +{ |
| 26 | + options.services.gemstash = { |
| 27 | + enable = mkEnableOption (lib.mdDoc "gemstash service"); |
| 28 | + |
| 29 | + openFirewall = mkOption { |
| 30 | + type = types.bool; |
| 31 | + default = false; |
| 32 | + description = lib.mdDoc '' |
| 33 | + Whether to open the firewall for the port in {option}`services.gemstash.bind`. |
| 34 | + ''; |
| 35 | + }; |
| 36 | + |
| 37 | + settings = mkOption { |
| 38 | + default = {}; |
| 39 | + description = lib.mdDoc '' |
| 40 | + Configuration for Gemstash. The details can be found at in |
| 41 | + [gemstash documentation](https://github.com/rubygems/gemstash/blob/master/man/gemstash-configuration.5.md). |
| 42 | + Each key set here is automatically prefixed with ":" to match the gemstash expectations. |
| 43 | + ''; |
| 44 | + type = types.submodule { |
| 45 | + freeformType = settingsFormat.type; |
| 46 | + options = { |
| 47 | + base_path = mkOption { |
| 48 | + type = types.path; |
| 49 | + default = "/var/lib/gemstash"; |
| 50 | + description = lib.mdDoc "Path to store the gem files and the sqlite database. If left unchanged, the directory will be created."; |
| 51 | + }; |
| 52 | + bind = mkOption { |
| 53 | + type = types.str; |
| 54 | + default = "tcp://0.0.0.0:9292"; |
| 55 | + description = lib.mdDoc "Host and port combination for the server to listen on."; |
| 56 | + }; |
| 57 | + db_adapter = mkOption { |
| 58 | + type = types.nullOr (types.enum [ "sqlite3" "postgres" "mysql" "mysql2" ]); |
| 59 | + default = null; |
| 60 | + description = lib.mdDoc "Which database type to use. For choices other than sqlite3, the dbUrl has to be specified as well."; |
| 61 | + }; |
| 62 | + db_url = mkOption { |
| 63 | + type = types.nullOr types.str; |
| 64 | + default = null; |
| 65 | + description = lib.mdDoc "The database to connect to when using postgres, mysql, or mysql2."; |
| 66 | + }; |
| 67 | + }; |
| 68 | + }; |
| 69 | + }; |
| 70 | + }; |
| 71 | + |
| 72 | + config = |
| 73 | + mkIf cfg.enable { |
| 74 | + users = { |
| 75 | + users.gemstash = { |
| 76 | + group = "gemstash"; |
| 77 | + isSystemUser = true; |
| 78 | + }; |
| 79 | + groups.gemstash = { }; |
| 80 | + }; |
| 81 | + |
| 82 | + networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ (parseBindPort cfg.settings.bind) ]; |
| 83 | + |
| 84 | + systemd.services.gemstash = { |
| 85 | + wantedBy = [ "multi-user.target" ]; |
| 86 | + after = [ "network.target" ]; |
| 87 | + serviceConfig = mkMerge [ |
| 88 | + { |
| 89 | + ExecStart = "${pkgs.gemstash}/bin/gemstash start --no-daemonize --config-file ${settingsFormat.generate "gemstash.yaml" (prefixColon cfg.settings)}"; |
| 90 | + NoNewPrivileges = true; |
| 91 | + User = "gemstash"; |
| 92 | + Group = "gemstash"; |
| 93 | + PrivateTmp = true; |
| 94 | + RestrictSUIDSGID = true; |
| 95 | + LockPersonality = true; |
| 96 | + } |
| 97 | + (mkIf (cfg.settings.base_path == "/var/lib/gemstash") { |
| 98 | + StateDirectory = "gemstash"; |
| 99 | + }) |
| 100 | + ]; |
| 101 | + }; |
| 102 | + }; |
| 103 | +} |
0 commit comments