nixos/atuin: fix `database.createLocally` behaviour

Co-authored-by: Andrew Marshall <andrew@johnandrewmarshall.com>

h7x4 fd01b3f5 025b5d48

Changed files
+22 -11
nixos
modules
services
misc
tests
+20 -11
nixos/modules/services/misc/atuin.nix
···
{ config, pkgs, lib, ... }:
-
-
with lib;
-
let
cfg = config.services.atuin;
in
{
options = {
services.atuin = {
-
enable = mkEnableOption (mdDoc "Enable server for shell history sync with atuin");
openRegistration = mkOption {
type = types.bool;
···
createLocally = mkOption {
type = types.bool;
default = true;
-
description = lib.mdDoc "Create the database and database user locally.";
};
};
};
};
config = mkIf cfg.enable {
-
# enable postgres to host atuin db
-
services.postgresql = {
enable = true;
ensureUsers = [{
name = "atuin";
···
systemd.services.atuin = {
description = "atuin server";
requires = lib.optionals cfg.database.createLocally [ "postgresql.service" ];
-
after = [ "network.target" ] ++ lib.optionals cfg.database.createLocally [ "postgresql.service" ] ;
wantedBy = [ "multi-user.target" ];
serviceConfig = {
···
ATUIN_HOST = cfg.host;
ATUIN_PORT = toString cfg.port;
ATUIN_MAX_HISTORY_LENGTH = toString cfg.maxHistoryLength;
-
ATUIN_OPEN_REGISTRATION = boolToString cfg.openRegistration;
-
ATUIN_DB_URI = mkIf cfg.database.createLocally "postgresql:///atuin";
ATUIN_PATH = cfg.path;
ATUIN_CONFIG_DIR = "/run/atuin"; # required to start, but not used as configuration is via environment variables
};
};
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
-
};
}
···
{ config, pkgs, lib, ... }:
let
+
inherit (lib) mkOption types mdDoc mkIf;
cfg = config.services.atuin;
in
{
options = {
services.atuin = {
+
enable = lib.mkEnableOption (mdDoc "Enable server for shell history sync with atuin");
openRegistration = mkOption {
type = types.bool;
···
createLocally = mkOption {
type = types.bool;
default = true;
+
description = mdDoc "Create the database and database user locally.";
+
};
+
+
uri = mkOption {
+
type = types.str;
+
default = "postgresql:///atuin?host=/run/postgresql";
+
example = "postgresql://atuin@localhost:5432/atuin";
+
description = mdDoc "URI to the database";
};
};
};
};
config = mkIf cfg.enable {
+
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 {
enable = true;
ensureUsers = [{
name = "atuin";
···
systemd.services.atuin = {
description = "atuin server";
requires = lib.optionals cfg.database.createLocally [ "postgresql.service" ];
+
after = [ "network.target" ] ++ lib.optionals cfg.database.createLocally [ "postgresql.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
···
ATUIN_HOST = cfg.host;
ATUIN_PORT = toString cfg.port;
ATUIN_MAX_HISTORY_LENGTH = toString cfg.maxHistoryLength;
+
ATUIN_OPEN_REGISTRATION = lib.boolToString cfg.openRegistration;
+
ATUIN_DB_URI = cfg.database.uri;
ATUIN_PATH = cfg.path;
ATUIN_CONFIG_DIR = "/run/atuin"; # required to start, but not used as configuration is via environment variables
};
};
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
};
}
+2
nixos/tests/atuin.nix
···
server =
{ ... }:
{
services.atuin = {
enable = true;
port = testPort;
···
server =
{ ... }:
{
+
services.postgresql.enable = true;
+
services.atuin = {
enable = true;
port = testPort;