2024-10-14 16:23:29 +00:00
|
|
|
{
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
config,
|
|
|
|
...
|
|
|
|
}:
|
2023-01-06 07:59:35 +00:00
|
|
|
|
|
|
|
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 { };
|
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
checkedConfigFile =
|
|
|
|
pkgs.runCommand "checked-attic-server.toml"
|
|
|
|
{
|
|
|
|
configFile = cfg.configFile;
|
|
|
|
}
|
|
|
|
''
|
|
|
|
cat $configFile
|
2023-01-06 07:59:35 +00:00
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
export ATTIC_SERVER_TOKEN_HS256_SECRET_BASE64="dGVzdCBzZWNyZXQ="
|
|
|
|
export ATTIC_SERVER_DATABASE_URL="sqlite://:memory:"
|
2024-10-14 16:23:29 +00:00
|
|
|
${lib.getExe cfg.package} --mode check-config -f $configFile
|
2024-10-14 16:23:29 +00:00
|
|
|
cat <$configFile >$out
|
|
|
|
'';
|
2023-01-06 07:59:35 +00:00
|
|
|
|
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-08-11 13:08:41 +00:00
|
|
|
--pipe \
|
2023-01-15 06:55:10 +00:00
|
|
|
--pty \
|
|
|
|
--wait \
|
|
|
|
--collect \
|
|
|
|
--service-type=exec \
|
2024-10-14 16:23:29 +00:00
|
|
|
--property=EnvironmentFile=${cfg.environmentFile} \
|
2023-01-15 06:55:10 +00:00
|
|
|
--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
|
|
|
'';
|
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
hasLocalPostgresDB =
|
|
|
|
let
|
|
|
|
url = cfg.settings.database.url or "";
|
|
|
|
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;
|
2023-01-06 07:59:35 +00:00
|
|
|
in
|
|
|
|
{
|
2024-10-14 16:23:29 +00:00
|
|
|
imports = [
|
|
|
|
(lib.mkRenamedOptionModule [ "services" "atticd" "credentialsFile" ] [ "services" "atticd" "environmentFile" ])
|
|
|
|
];
|
|
|
|
|
2023-01-06 07:59:35 +00:00
|
|
|
options = {
|
|
|
|
services.atticd = {
|
2024-10-14 16:23:29 +00:00
|
|
|
enable = lib.mkEnableOption "the atticd, the Nix Binary Cache server";
|
|
|
|
|
|
|
|
package = lib.mkPackageOption pkgs "attic-server" { };
|
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
environmentFile = lib.mkOption {
|
2023-01-06 07:59:35 +00:00
|
|
|
description = ''
|
|
|
|
Path to an EnvironmentFile containing required environment
|
|
|
|
variables:
|
|
|
|
|
2023-11-08 16:42:38 +00:00
|
|
|
- ATTIC_SERVER_TOKEN_RS256_SECRET_BASE64: The base64-encoded RSA PEM PKCS1 of the
|
|
|
|
RS256 JWT secret. Generate it with `openssl genrsa -traditional 4096 | 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
|
|
|
};
|
2024-10-14 16:23:29 +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";
|
|
|
|
};
|
2024-10-14 16:23:29 +00:00
|
|
|
|
2023-03-02 09:14:08 +00:00
|
|
|
group = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
The user under which attic runs.
|
|
|
|
'';
|
|
|
|
type = types.str;
|
|
|
|
default = "atticd";
|
|
|
|
};
|
2024-10-14 16:23:29 +00:00
|
|
|
|
2023-01-06 07:59:35 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
description = ''
|
|
|
|
Structured configurations of atticd.
|
|
|
|
'';
|
|
|
|
type = format.type;
|
2024-10-14 16:23:29 +00:00
|
|
|
default = { }; # setting defaults here does not compose well
|
2023-01-06 07:59:35 +00:00
|
|
|
};
|
2024-10-14 16:23:29 +00:00
|
|
|
|
2023-01-06 07:59:35 +00:00
|
|
|
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.
|
|
|
|
'';
|
2024-10-14 16:23:29 +00:00
|
|
|
type = lib.types.enum [
|
|
|
|
"monolithic"
|
|
|
|
"api-server"
|
|
|
|
"garbage-collector"
|
|
|
|
];
|
2023-07-22 15:45:06 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
2024-10-14 16:23:29 +00:00
|
|
|
assertion = cfg.environmentFile != null;
|
2024-10-14 16:23:29 +00:00
|
|
|
message = ''
|
2024-10-14 16:23:29 +00:00
|
|
|
<option>services.atticd.environmentFile</option> is not set.
|
2024-10-14 16:23:29 +00:00
|
|
|
|
|
|
|
Run `openssl genrsa -traditional -out private_key.pem 4096 | base64 -w0` and create a file with the following contents:
|
|
|
|
|
|
|
|
ATTIC_SERVER_TOKEN_RS256_SECRET="output from command"
|
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
Then, set `services.atticd.environmentFile` to the quoted absolute path of the file.
|
2024-10-14 16:23:29 +00:00
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
2024-10-14 16:23:29 +00:00
|
|
|
assertion = !lib.isStorePath cfg.environmentFile;
|
2024-10-14 16:23:29 +00:00
|
|
|
message = ''
|
2024-10-14 16:23:29 +00:00
|
|
|
<option>services.atticd.environmentFile</option> points to a path in the Nix store. The Nix store is globally readable.
|
2024-10-14 16:23:29 +00:00
|
|
|
|
2024-10-14 21:10:21 +00:00
|
|
|
You should use a quoted absolute path to prevent leaking secrets in the Nix store.
|
2024-10-14 16:23:29 +00:00
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
2023-01-06 07:59:35 +00:00
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
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";
|
2023-01-06 07:59:35 +00:00
|
|
|
};
|
2024-10-14 16:23:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.atticd = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2024-10-14 16:23:29 +00:00
|
|
|
after = [ "network-online.target" ] ++ lib.optionals hasLocalPostgresDB [ "postgresql.service" ];
|
|
|
|
requires = lib.optionals hasLocalPostgresDB [ "postgresql.service" ];
|
2024-10-29 18:25:02 +00:00
|
|
|
wants = [ "network-online.target" ];
|
2023-01-06 07:59:35 +00:00
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
serviceConfig = {
|
2024-10-14 16:23:29 +00:00
|
|
|
ExecStart = "${lib.getExe cfg.package} -f ${checkedConfigFile} --mode ${cfg.mode}";
|
2024-10-14 16:23:29 +00:00
|
|
|
EnvironmentFile = cfg.environmentFile;
|
2024-10-14 16:23:29 +00:00
|
|
|
StateDirectory = "atticd"; # for usage with local storage and sqlite
|
|
|
|
DynamicUser = true;
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 10;
|
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
CapabilityBoundingSet = [ "" ];
|
|
|
|
DeviceAllow = "";
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProcSubset = "pid";
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
2024-10-14 16:23:29 +00:00
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ReadWritePaths =
|
|
|
|
let
|
2023-05-22 16:50:48 +00:00
|
|
|
path = cfg.settings.storage.path;
|
|
|
|
isDefaultStateDirectory = path == "/var/lib/atticd" || lib.hasPrefix "/var/lib/atticd/" path;
|
2024-10-14 16:23:29 +00:00
|
|
|
in
|
|
|
|
lib.optionals (cfg.settings.storage.type or "" == "local" && !isDefaultStateDirectory) [ path ];
|
2024-10-14 16:23:29 +00:00
|
|
|
RemoveIPC = true;
|
2024-10-14 16:23:29 +00:00
|
|
|
RestrictAddressFamilies = [
|
|
|
|
"AF_INET"
|
|
|
|
"AF_INET6"
|
|
|
|
"AF_UNIX"
|
|
|
|
];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
2024-10-14 16:23:29 +00:00
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = [
|
|
|
|
"@system-service"
|
|
|
|
"~@resources"
|
|
|
|
"~@privileged"
|
|
|
|
];
|
|
|
|
UMask = "0077";
|
2023-01-06 07:59:35 +00:00
|
|
|
};
|
2024-10-14 16:23:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
environment.systemPackages = [
|
|
|
|
atticadmWrapper
|
|
|
|
];
|
2023-01-06 07:59:35 +00:00
|
|
|
|
2024-10-14 16:23:29 +00:00
|
|
|
nixpkgs.overlays = lib.mkIf cfg.useFlakeCompatOverlay [
|
|
|
|
overlay
|
|
|
|
];
|
|
|
|
};
|
2023-01-06 07:59:35 +00:00
|
|
|
}
|