8 settingsFormat = pkgs.formats.yaml { };
10 # gemstash uses a yaml config where the keys are ruby symbols,
11 # which means they start with ':'. This would be annoying to use
12 # on the nix side, so we rewrite plain names instead.
17 name = ":${attrName}";
18 value = if lib.isAttrs s.${attrName} then prefixColon s."${attrName}" else s."${attrName}";
22 # parse the port number out of the tcp://ip:port bind setting string
23 parseBindPort = bind: lib.strings.toInt (lib.last (lib.strings.splitString ":" bind));
25 cfg = config.services.gemstash;
28 options.services.gemstash = {
29 enable = lib.mkEnableOption "gemstash, a cache for rubygems.org and a private gem server";
31 openFirewall = lib.mkOption {
32 type = lib.types.bool;
35 Whether to open the firewall for the port in {option}`services.gemstash.bind`.
39 settings = lib.mkOption {
42 Configuration for Gemstash. The details can be found at in
43 [gemstash documentation](https://github.com/rubygems/gemstash/blob/master/man/gemstash-configuration.5.md).
44 Each key set here is automatically prefixed with ":" to match the gemstash expectations.
46 type = lib.types.submodule {
47 freeformType = settingsFormat.type;
49 base_path = lib.mkOption {
50 type = lib.types.path;
51 default = "/var/lib/gemstash";
52 description = "Path to store the gem files and the sqlite database. If left unchanged, the directory will be created.";
56 default = "tcp://0.0.0.0:9292";
57 description = "Host and port combination for the server to listen on.";
59 db_adapter = lib.mkOption {
60 type = lib.types.nullOr (
69 description = "Which database type to use. For choices other than sqlite3, the dbUrl has to be specified as well.";
71 db_url = lib.mkOption {
72 type = lib.types.nullOr lib.types.str;
74 description = "The database to connect to when using postgres, mysql, or mysql2.";
81 config = lib.mkIf cfg.enable {
87 groups.gemstash = { };
90 networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [
91 (parseBindPort cfg.settings.bind)
94 systemd.services.gemstash = {
95 wantedBy = [ "multi-user.target" ];
96 after = [ "network.target" ];
97 serviceConfig = lib.mkMerge [
99 ExecStart = "${pkgs.gemstash}/bin/gemstash start --no-daemonize --config-file ${settingsFormat.generate "gemstash.yaml" (prefixColon cfg.settings)}";
100 NoNewPrivileges = true;
104 RestrictSUIDSGID = true;
105 LockPersonality = true;
107 (lib.mkIf (cfg.settings.base_path == "/var/lib/gemstash") {
108 StateDirectory = "gemstash";