Skip to content

Commit 7007398

Browse files
committed
nixos/gemstash: init module
1 parent b6cc2f2 commit 7007398

6 files changed

Lines changed: 163 additions & 2 deletions

File tree

nixos/doc/manual/release-notes/rl-2305.section.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ In addition to numerous new and upgraded packages, this release has the followin
3434

3535
- [fzf](https://github.com/junegunn/fzf), a command line fuzzyfinder. Available as [programs.fzf](#opt-programs.fzf.fuzzyCompletion).
3636

37+
- [gemstash](https://github.com/rubygems/gemstash), a RubyGems.org cache and private gem server. Available as [services.gemstash](#opt-services.gemstash.enable).
38+
3739
- [gmediarender](https://github.com/hzeller/gmrender-resurrect), a simple, headless UPnP/DLNA renderer. Available as [services.gmediarender](options.html#opt-services.gmediarender.enable).
3840

3941
- [stevenblack-blocklist](https://github.com/StevenBlack/hosts), A unified hosts file with base extensions for blocking unwanted websites. Available as [networking.stevenblack](options.html#opt-networking.stevenblack.enable).

nixos/modules/module-list.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@
439439
./services/development/blackfire.nix
440440
./services/development/bloop.nix
441441
./services/development/distccd.nix
442+
./services/development/gemstash.nix
442443
./services/development/hoogle.nix
443444
./services/development/jupyter/default.nix
444445
./services/development/jupyterhub/default.nix
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

nixos/tests/all-tests.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ in {
238238
ft2-clone = handleTest ./ft2-clone.nix {};
239239
mimir = handleTest ./mimir.nix {};
240240
garage = handleTest ./garage {};
241+
gemstash = handleTest ./gemstash.nix {};
241242
gerrit = handleTest ./gerrit.nix {};
242243
geth = handleTest ./geth.nix {};
243244
ghostunnel = handleTest ./ghostunnel.nix {};

nixos/tests/gemstash.nix

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{ system ? builtins.currentSystem, config ? { }
2+
, pkgs ? import ../.. { inherit system config; } }:
3+
4+
with import ../lib/testing-python.nix { inherit system pkgs; };
5+
with pkgs.lib;
6+
7+
let common_meta = { maintainers = [ maintainers.viraptor ]; };
8+
in
9+
{
10+
gemstash_works = makeTest {
11+
name = "gemstash-works";
12+
meta = common_meta;
13+
14+
nodes.machine = { config, pkgs, ... }: {
15+
services.gemstash = {
16+
enable = true;
17+
};
18+
};
19+
20+
# gemstash responds to http requests
21+
testScript = ''
22+
machine.wait_for_unit("gemstash.service")
23+
machine.wait_for_file("/var/lib/gemstash")
24+
machine.wait_for_open_port(9292)
25+
machine.succeed("curl http://localhost:9292")
26+
'';
27+
};
28+
29+
gemstash_custom_port = makeTest {
30+
name = "gemstash-custom-port";
31+
meta = common_meta;
32+
33+
nodes.machine = { config, pkgs, ... }: {
34+
services.gemstash = {
35+
enable = true;
36+
openFirewall = true;
37+
settings = {
38+
bind = "tcp://0.0.0.0:12345";
39+
};
40+
};
41+
};
42+
43+
# gemstash responds to http requests
44+
testScript = ''
45+
machine.wait_for_unit("gemstash.service")
46+
machine.wait_for_file("/var/lib/gemstash")
47+
machine.wait_for_open_port(12345)
48+
machine.succeed("curl http://localhost:12345")
49+
'';
50+
};
51+
}

pkgs/development/tools/gemstash/default.nix

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
{ lib, bundlerApp, bundlerUpdateScript }:
1+
{ lib, bundlerApp, bundlerUpdateScript, nixosTests }:
22

33
bundlerApp rec {
44
pname = "gemstash";
55
gemdir = ./.;
66
exes = [ pname ];
77

8-
passthru.updateScript = bundlerUpdateScript pname;
8+
passthru = {
9+
updateScript = bundlerUpdateScript pname;
10+
tests = { inherit (nixosTests) gemstash; };
11+
};
912

1013
meta = with lib; {
1114
description = "A cache for RubyGems.org and a private gem server";

0 commit comments

Comments
 (0)