2023-01-06 07:59:35 +00:00
|
|
|
{ lib, pkgs, config, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (lib) types;
|
|
|
|
|
|
|
|
cfg = config.services.atticd;
|
|
|
|
|
|
|
|
# unused when the entrypoint is flake
|
|
|
|
flake = import ../flake-compat.nix;
|
|
|
|
overlay = flake.defaultNix.overlays.default;
|
|
|
|
|
|
|
|
format = pkgs.formats.toml { };
|
|
|
|
|
|
|
|
checkedConfigFile = pkgs.runCommand "checked-attic-server.toml" {
|
|
|
|
configFile = cfg.configFile;
|
|
|
|
} ''
|
|
|
|
cat $configFile
|
|
|
|
|
|
|
|
export ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64="dGVzdCBzZWNyZXQ="
|
2023-01-15 10:05:10 +00:00
|
|
|
export ATTIC_SERVER_DATABASE_URL="sqlite://:memory:"
|
2023-01-06 07:59:35 +00:00
|
|
|
${cfg.package}/bin/atticd --mode check-config -f $configFile
|
|
|
|
cat <$configFile >$out
|
|
|
|
'';
|
|
|
|
|
2023-02-16 21:01:27 +00:00
|
|
|
atticadmShim = pkgs.writeShellScript "atticadm" ''
|
|
|
|
if [ -n "$ATTICADM_PWD" ]; then
|
|
|
|
cd "$ATTICADM_PWD"
|
|
|
|
if [ "$?" != "0" ]; then
|
|
|
|
>&2 echo "Warning: Failed to change directory to $ATTICADM_PWD"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
exec ${cfg.package}/bin/atticadm -f ${checkedConfigFile} "$@"
|
|
|
|
'';
|
|
|
|
|
2023-01-15 06:55:10 +00:00
|
|
|
atticadmWrapper = pkgs.writeShellScriptBin "atticd-atticadm" ''
|
|
|
|
exec systemd-run \
|
2023-03-02 12:27:43 +00:00
|
|
|
--quiet \
|
2023-01-15 06:55:10 +00:00
|
|
|
--pty \
|
|
|
|
--same-dir \
|
|
|
|
--wait \
|
|
|
|
--collect \
|
|
|
|
--service-type=exec \
|
|
|
|
--property=EnvironmentFile=${cfg.credentialsFile} \
|
|
|
|
--property=DynamicUser=yes \
|
2023-08-16 22:30:40 +00:00
|
|
|
--property=User=${cfg.user} \
|
2023-02-16 21:01:27 +00:00
|
|
|
--property=Environment=ATTICADM_PWD=$(pwd) \
|
|
|
|
--working-directory / \
|
2023-01-15 06:55:10 +00:00
|
|
|
-- \
|
2023-02-16 21:01:27 +00:00
|
|
|
${atticadmShim} "$@"
|
2023-01-15 06:55:10 +00:00
|
|
|
'';
|
|
|
|
|
2023-01-06 07:59:35 +00:00
|
|
|
hasLocalPostgresDB = let
|
2023-01-15 10:05:10 +00:00
|
|
|
url = cfg.settings.database.url or "";
|
2023-01-06 07:59:35 +00:00
|
|
|
localStrings = [ "localhost" "127.0.0.1" "/run/postgresql" ];
|
|
|
|
hasLocalStrings = lib.any (lib.flip lib.hasInfix url) localStrings;
|
|
|
|
in config.services.postgresql.enable && lib.hasPrefix "postgresql://" url && hasLocalStrings;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.atticd = {
|
|
|
|
enable = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Whether to enable the atticd, the Nix Binary Cache server.
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
package = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
The package to use.
|
|
|
|
'';
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.attic-server;
|
|
|
|
};
|
|
|
|
credentialsFile = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Path to an EnvironmentFile containing required environment
|
|
|
|
variables:
|
|
|
|
|
|
|
|
- ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64: The Base64-encoded version of the
|
2023-01-15 06:55:10 +00:00
|
|
|
HS256 JWT secret. Generate it with `openssl rand 64 | base64 -w0`.
|
2023-01-06 07:59:35 +00:00
|
|
|
'';
|
2023-01-15 06:55:10 +00:00
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2023-01-06 07:59:35 +00:00
|
|
|
};
|
2023-03-02 09:14:08 +00:00
|
|
|
user = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
The group under which attic runs.
|
|
|
|
'';
|
|
|
|
type = types.str;
|
|
|
|
default = "atticd";
|
|
|
|
};
|
|
|
|
group = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
The user under which attic runs.
|
|
|
|
'';
|
|
|
|
type = types.str;
|
|
|
|
default = "atticd";
|
|
|
|
};
|
2023-01-06 07:59:35 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Structured configurations of atticd.
|
|
|
|
'';
|
|
|
|
type = format.type;
|
|
|
|
default = {}; # setting defaults here does not compose well
|
|
|
|
};
|
|
|
|
configFile = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Path to an existing atticd configuration file.
|
|
|
|
|
|
|
|
By default, it's generated from `services.atticd.settings`.
|
|
|
|
'';
|
|
|
|
type = types.path;
|
|
|
|
default = format.generate "server.toml" cfg.settings;
|
|
|
|
defaultText = "generated from `services.atticd.settings`";
|
|
|
|
};
|
|
|
|
|
2023-07-22 15:45:06 +00:00
|
|
|
mode = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Mode in which to run the server.
|
|
|
|
|
|
|
|
'monolithic' runs all components, and is suitable for single-node deployments.
|
|
|
|
|
|
|
|
'api-server' runs only the API server, and is suitable for clustering.
|
|
|
|
|
|
|
|
'garbage-collector' only runs the garbage collector periodically.
|
|
|
|
|
|
|
|
A simple NixOS-based Attic deployment will typically have one 'monolithic' and any number of 'api-server' nodes.
|
|
|
|
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
type = lib.types.enum ["monolithic" "api-server" "garbage-collector"];
|
|
|
|
default = "monolithic";
|
|
|
|
};
|
|
|
|
|
2023-01-06 07:59:35 +00:00
|
|
|
# Internal flags
|
|
|
|
useFlakeCompatOverlay = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Whether to insert the overlay with flake-compat.
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
internal = true;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = lib.mkIf (cfg.enable) (lib.mkMerge [
|
|
|
|
{
|
|
|
|
assertions = [
|
2023-01-15 06:55:10 +00:00
|
|
|
{
|
|
|
|
assertion = cfg.credentialsFile != null;
|
|
|
|
message = ''
|
|
|
|
<option>services.atticd.credentialsFile</option> is not set.
|
|
|
|
|
|
|
|
Run `openssl rand 64 | base64 -w0` and create a file with the following contents:
|
|
|
|
|
|
|
|
ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64="output from command"
|
|
|
|
|
|
|
|
Then, set `services.atticd.credentialsFile` to the quoted absolute path of the file.
|
|
|
|
'';
|
|
|
|
}
|
2023-01-06 07:59:35 +00:00
|
|
|
{
|
|
|
|
assertion = !lib.isStorePath cfg.credentialsFile;
|
|
|
|
message = ''
|
|
|
|
<option>services.atticd.credentialsFile</option> points to a path in the Nix store. The Nix store is globally readable.
|
|
|
|
|
|
|
|
You should use a quoted absolute path to prevent this.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services.atticd.settings = {
|
|
|
|
database.url = lib.mkDefault "sqlite:///var/lib/atticd/server.db?mode=rwc";
|
|
|
|
|
|
|
|
# "storage" is internally tagged
|
|
|
|
# if the user sets something the whole thing must be replaced
|
|
|
|
storage = lib.mkDefault {
|
|
|
|
type = "local";
|
|
|
|
path = "/var/lib/atticd/storage";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.atticd = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2023-03-05 18:05:11 +00:00
|
|
|
after = [ "network.target" ]
|
|
|
|
++ lib.optionals hasLocalPostgresDB [ "postgresql.service" "nss-lookup.target" ];
|
2023-01-06 07:59:35 +00:00
|
|
|
serviceConfig = {
|
2023-07-22 15:45:06 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/atticd -f ${checkedConfigFile} --mode ${cfg.mode}";
|
2023-01-06 07:59:35 +00:00
|
|
|
EnvironmentFile = cfg.credentialsFile;
|
|
|
|
StateDirectory = "atticd"; # for usage with local storage and sqlite
|
|
|
|
DynamicUser = true;
|
2023-03-02 09:14:08 +00:00
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
2023-01-06 07:59:35 +00:00
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
2023-05-22 16:50:48 +00:00
|
|
|
ReadWritePaths = let
|
|
|
|
path = cfg.settings.storage.path;
|
|
|
|
isDefaultStateDirectory = path == "/var/lib/atticd" || lib.hasPrefix "/var/lib/atticd/" path;
|
|
|
|
in lib.optionals (cfg.settings.storage.type or "" == "local" && !isDefaultStateDirectory) [ path ];
|
2023-01-06 07:59:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-01-15 06:55:10 +00:00
|
|
|
environment.systemPackages = [ atticadmWrapper ];
|
2023-01-06 07:59:35 +00:00
|
|
|
}
|
|
|
|
(lib.mkIf cfg.useFlakeCompatOverlay {
|
|
|
|
nixpkgs.overlays = [ overlay ];
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
}
|