11 cfg = config.services.atticd;
13 format = pkgs.formats.toml { };
16 pkgs.runCommand "checked-attic-server.toml"
18 configFile = format.generate "server.toml" cfg.settings;
21 export ATTIC_SERVER_TOKEN_RS256_SECRET_BASE64="$(${lib.getExe pkgs.openssl} genrsa -traditional 4096 | ${pkgs.coreutils}/bin/base64 -w0)"
22 export ATTIC_SERVER_DATABASE_URL="sqlite://:memory:"
23 ${lib.getExe cfg.package} --mode check-config -f $configFile
24 cat <$configFile >$out
27 atticadmShim = pkgs.writeShellScript "atticadm" ''
28 if [ -n "$ATTICADM_PWD" ]; then
30 if [ "$?" != "0" ]; then
31 >&2 echo "Warning: Failed to change directory to $ATTICADM_PWD"
35 exec ${cfg.package}/bin/atticadm -f ${checkedConfigFile} "$@"
38 atticadmWrapper = pkgs.writeShellScriptBin "atticd-atticadm" ''
47 --property=EnvironmentFile=${cfg.environmentFile} \
48 --property=DynamicUser=yes \
49 --property=User=${cfg.user} \
50 --property=Environment=ATTICADM_PWD=$(pwd) \
51 --working-directory / \
58 url = cfg.settings.database.url or "";
64 hasLocalStrings = lib.any (lib.flip lib.hasInfix url) localStrings;
66 config.services.postgresql.enable && lib.hasPrefix "postgresql://" url && hasLocalStrings;
71 enable = lib.mkEnableOption "the atticd, the Nix Binary Cache server";
73 package = lib.mkPackageOption pkgs "attic-server" { };
75 environmentFile = lib.mkOption {
77 Path to an EnvironmentFile containing required environment
80 - ATTIC_SERVER_TOKEN_RS256_SECRET_BASE64: The base64-encoded RSA PEM PKCS1 of the
81 RS256 JWT secret. Generate it with `openssl genrsa -traditional 4096 | base64 -w0`.
83 type = types.nullOr types.path;
89 The group under which attic runs.
95 group = lib.mkOption {
97 The user under which attic runs.
103 settings = lib.mkOption {
105 Structured configurations of atticd.
106 See https://github.com/zhaofengli/attic/blob/main/server/src/config-template.toml
112 mode = lib.mkOption {
114 Mode in which to run the server.
116 'monolithic' runs all components, and is suitable for single-node deployments.
118 'api-server' runs only the API server, and is suitable for clustering.
120 'garbage-collector' only runs the garbage collector periodically.
122 A simple NixOS-based Attic deployment will typically have one 'monolithic' and any number of 'api-server' nodes.
124 There are several other supported modes that perform one-off operations, but these are the only ones that make sense to run via the NixOS module.
126 type = lib.types.enum [
131 default = "monolithic";
136 config = lib.mkIf cfg.enable {
139 assertion = cfg.environmentFile != null;
141 <option>services.atticd.environmentFile</option> is not set.
143 Run `openssl genrsa -traditional 4096 | base64 -w0` and create a file with the following contents:
145 ATTIC_SERVER_TOKEN_RS256_SECRET="output from command"
147 Then, set `services.atticd.environmentFile` to the quoted absolute path of the file.
152 services.atticd.settings = {
153 chunking = lib.mkDefault {
154 nar-size-threshold = 65536;
155 min-size = 16384; # 16 KiB
156 avg-size = 65536; # 64 KiB
157 max-size = 262144; # 256 KiB
160 database.url = lib.mkDefault "sqlite:///var/lib/atticd/server.db?mode=rwc";
162 # "storage" is internally tagged
163 # if the user sets something the whole thing must be replaced
164 storage = lib.mkDefault {
166 path = "/var/lib/atticd/storage";
170 systemd.services.atticd = {
171 wantedBy = [ "multi-user.target" ];
172 after = [ "network-online.target" ] ++ lib.optionals hasLocalPostgresDB [ "postgresql.service" ];
173 requires = lib.optionals hasLocalPostgresDB [ "postgresql.service" ];
174 wants = [ "network-online.target" ];
177 ExecStart = "${lib.getExe cfg.package} -f ${checkedConfigFile} --mode ${cfg.mode}";
178 EnvironmentFile = cfg.environmentFile;
179 StateDirectory = "atticd"; # for usage with local storage and sqlite
183 Restart = "on-failure";
186 CapabilityBoundingSet = [ "" ];
188 DevicePolicy = "closed";
189 LockPersonality = true;
190 MemoryDenyWriteExecute = true;
191 NoNewPrivileges = true;
192 PrivateDevices = true;
197 ProtectControlGroups = true;
199 ProtectHostname = true;
200 ProtectKernelLogs = true;
201 ProtectKernelModules = true;
202 ProtectKernelTunables = true;
203 ProtectProc = "invisible";
204 ProtectSystem = "strict";
207 path = cfg.settings.storage.path;
208 isDefaultStateDirectory = path == "/var/lib/atticd" || lib.hasPrefix "/var/lib/atticd/" path;
210 lib.optionals (cfg.settings.storage.type or "" == "local" && !isDefaultStateDirectory) [ path ];
212 RestrictAddressFamilies = [
217 RestrictNamespaces = true;
218 RestrictRealtime = true;
219 RestrictSUIDSGID = true;
220 SystemCallArchitectures = "native";
230 environment.systemPackages = [