2022-12-17 10:02:37 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
let
|
2023-08-04 22:07:22 +00:00
|
|
|
inherit (lib) mkOption types mdDoc mkIf;
|
2022-12-17 10:02:37 +00:00
|
|
|
cfg = config.services.atuin;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.atuin = {
|
2023-10-09 19:29:22 +00:00
|
|
|
enable = lib.mkEnableOption (mdDoc "Atuin server for shell history sync");
|
2022-12-17 10:02:37 +00:00
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
package = lib.mkPackageOption pkgs "atuin" { };
|
|
|
|
|
2022-12-17 10:02:37 +00:00
|
|
|
openRegistration = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = mdDoc "Allow new user registrations with the atuin server.";
|
|
|
|
};
|
|
|
|
|
|
|
|
path = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
|
|
|
description = mdDoc "A path to prepend to all the routes of the server.";
|
|
|
|
};
|
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
description = mdDoc "The host address the atuin server should listen on.";
|
|
|
|
};
|
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
maxHistoryLength = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 8192;
|
|
|
|
description = mdDoc "The max length of each history item the atuin server should store.";
|
|
|
|
};
|
|
|
|
|
2022-12-17 10:02:37 +00:00
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 8888;
|
|
|
|
description = mdDoc "The port the atuin server should listen on.";
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = mdDoc "Open ports in the firewall for the atuin server.";
|
|
|
|
};
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
database = {
|
|
|
|
createLocally = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-08-04 22:07:22 +00:00
|
|
|
description = mdDoc "Create the database and database user locally.";
|
|
|
|
};
|
|
|
|
|
|
|
|
uri = mkOption {
|
2024-02-29 20:09:43 +00:00
|
|
|
type = types.nullOr types.str;
|
2023-08-04 22:07:22 +00:00
|
|
|
default = "postgresql:///atuin?host=/run/postgresql";
|
|
|
|
example = "postgresql://atuin@localhost:5432/atuin";
|
2024-02-29 20:09:43 +00:00
|
|
|
description = mdDoc ''
|
|
|
|
URI to the database.
|
|
|
|
Can be set to null in which case ATUIN_DB_URI should be set through an EnvironmentFile
|
|
|
|
'';
|
2023-07-15 17:15:38 +00:00
|
|
|
};
|
|
|
|
};
|
2022-12-17 10:02:37 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2023-08-04 22:07:22 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.database.createLocally -> config.services.postgresql.enable;
|
|
|
|
message = "Postgresql must be enabled to create a local database";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services.postgresql = mkIf cfg.database.createLocally {
|
2022-12-17 10:02:37 +00:00
|
|
|
enable = true;
|
|
|
|
ensureUsers = [{
|
|
|
|
name = "atuin";
|
2024-01-02 11:29:13 +00:00
|
|
|
ensureDBOwnership = true;
|
2022-12-17 10:02:37 +00:00
|
|
|
}];
|
|
|
|
ensureDatabases = [ "atuin" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.atuin = {
|
|
|
|
description = "atuin server";
|
2023-07-15 17:15:38 +00:00
|
|
|
requires = lib.optionals cfg.database.createLocally [ "postgresql.service" ];
|
2023-08-04 22:07:22 +00:00
|
|
|
after = [ "network.target" ] ++ lib.optionals cfg.database.createLocally [ "postgresql.service" ];
|
2022-12-17 10:02:37 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
2024-02-29 20:09:43 +00:00
|
|
|
ExecStart = "${lib.getExe cfg.package} server start";
|
2022-12-17 10:02:37 +00:00
|
|
|
RuntimeDirectory = "atuin";
|
|
|
|
RuntimeDirectoryMode = "0700";
|
|
|
|
DynamicUser = true;
|
2023-08-04 22:07:22 +00:00
|
|
|
|
|
|
|
# Hardening
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
LockPersonality = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProcSubset = "pid";
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProtectSystem = "full";
|
|
|
|
RemoveIPC = true;
|
|
|
|
RestrictAddressFamilies = [
|
|
|
|
"AF_INET"
|
|
|
|
"AF_INET6"
|
|
|
|
# Required for connecting to database sockets,
|
|
|
|
"AF_UNIX"
|
|
|
|
];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = [
|
|
|
|
"@system-service"
|
|
|
|
"~@privileged"
|
|
|
|
];
|
|
|
|
UMask = "0077";
|
2022-12-17 10:02:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
environment = {
|
|
|
|
ATUIN_HOST = cfg.host;
|
|
|
|
ATUIN_PORT = toString cfg.port;
|
2023-04-12 12:48:02 +00:00
|
|
|
ATUIN_MAX_HISTORY_LENGTH = toString cfg.maxHistoryLength;
|
2023-08-04 22:07:22 +00:00
|
|
|
ATUIN_OPEN_REGISTRATION = lib.boolToString cfg.openRegistration;
|
2022-12-17 10:02:37 +00:00
|
|
|
ATUIN_PATH = cfg.path;
|
|
|
|
ATUIN_CONFIG_DIR = "/run/atuin"; # required to start, but not used as configuration is via environment variables
|
2024-02-29 20:09:43 +00:00
|
|
|
} // lib.optionalAttrs (cfg.database.uri != null) {
|
|
|
|
ATUIN_DB_URI = cfg.database.uri;
|
2022-12-17 10:02:37 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
|
|
|
|
};
|
|
|
|
}
|