Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions modules/lib/maintainers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@
githubId = 46252070;
name = "Sara Johnsson";
};
fk29g = {
name = "nicknb";
email = "nicknb@posteo.com";
github = "fk29g";
githubId = 205353326;
keys = [
{
longKeyId = "ed25519/0xD90384C807BC1FE6";
fingerprint = "8FF0 1BEE BCBF 1D20 ECD3 5FFA D903 84C8 07BC 1FE6";
}
{
longKeyId = "ed25519/0x2BF0FBB405AD6A7C";
fingerprint = "6F9B 47B6 FFB0 823D 2ACF 9ED8 2BF0 FBB4 05AD 6A7C";
}
];
};
florpe = {
email = "jens.krewald@gmail.com";
github = "florpe";
Expand Down
7 changes: 7 additions & 0 deletions modules/misc/news/2025/10/2025-10-12_18-29-08.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
time = "2025-10-12T16:29:08+00:00";
condition = true;
message = ''
A new module is available: `programs.sqlite`.
'';
}
136 changes: 136 additions & 0 deletions modules/programs/sqlite.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
lib,
config,
pkgs,
...
}:

let
cfg = config.programs.sqlite;
defaultColSeparator = "|";
defaultPrompt = "sqlite> ";
in
{
meta.maintainers = [ lib.hm.maintainers.fk29g ];

options.programs.sqlite = {
enable = lib.mkEnableOption "sqlite";

package = lib.mkPackageOption pkgs "sqlite-interactive" {
nullable = true;
example = [ "sqlite" ];
};

mode = lib.mkOption {
type = lib.types.nullOr (
lib.types.enum [
"ascii"
"box"
"column"
"csv"
"html"
"insert"
"json"
"line"
"list"
"markdown"
"qbox"
"quote"
"table"
"tabs"
"tcl"
]
);
default = null;
description = ''
Output format for query results.

If set to `null`, SQLite uses "list".
'';
};

separator = {
column = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = " | ";
description = ''
Column separator.

If set to `null`, SQLite uses "${defaultColSeparator}".
'';
};

row = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "\\n\\n";
description = ''
Row separator.

If set to `null`, SQLite uses "\n".
'';
};
};

prompt = {
main = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "❯ ";
description = ''
Main shell prompt.

If set to `null`, SQLite uses `${defaultPrompt}`.
'';
};

continue = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
Shell prompt for continuing lines of multi-line queries.

If set to `null`, SQLite uses ` ...> `.
'';
};
};

extraConfig = lib.mkOption {
type = lib.types.lines;
default = "";
description = ''
Extra lines added to {file}`sqliterc`.
'';
};
};

config = lib.mkIf cfg.enable {
home.packages = lib.mkIf (cfg.package != null) [ cfg.package ];

xdg.configFile."sqlite3/sqliterc".text = lib.concatStringsSep "\n" (
lib.filter (x: x != "") [
(lib.optionalString (cfg.mode != null) ".mode ${cfg.mode}")
(lib.optionalString (
cfg.separator.column != null && cfg.separator.row != null
) ".separator \"${cfg.separator.column}\" \"${cfg.separator.row}\"")
(lib.optionalString (
cfg.separator.column != null && cfg.separator.row == null
) ".separator \"${cfg.separator.column}\"")
(lib.optionalString (
cfg.separator.column == null && cfg.separator.row != null
) ".separator \"${defaultColSeparator}\" \"${cfg.separator.row}\"")
(lib.optionalString (
cfg.prompt.main != null && cfg.prompt.continue != null
) ".prompt \"${cfg.prompt.main}\" \"${cfg.prompt.continue}\"")
(lib.optionalString (
cfg.prompt.main != null && cfg.prompt.continue == null
) ".prompt \"${cfg.prompt.main}\"")
(lib.optionalString (
cfg.prompt.main == null && cfg.prompt.continue != null
) ".prompt \"${defaultPrompt}\" \"${cfg.prompt.continue}\"")
(lib.optionalString (cfg.extraConfig != "") (cfg.extraConfig))
]
);
Comment on lines +111 to +134
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry about this being too restrictive and also dismissive of configuration. This contains a lot of conditional logic when it seems like the file is a basic syntax of .<option> key value or something? Maybe we can make/use a generator that allows more freeform configuration?

If not interested in a generator, I think it'd make more sense to create warnings/assertions when combining mutually exclusive options instead of just silently choosing which to apply.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, it should probably have an escape hatch of some sort. This is not a fancy looking declarative configuration, it's just a script that gets run upon running sqlite. It can include arbitrary SQL in addition to the . commands. It's pretty common to use it to enable foreign key constraints for example.

};
}
4 changes: 4 additions & 0 deletions tests/modules/programs/sqlite/basic-configuration-expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.mode box
.separator " | " "\n\n"
.prompt "> " "..> "
.help
26 changes: 26 additions & 0 deletions tests/modules/programs/sqlite/basic-configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
programs.sqlite = {
enable = true;
mode = "box";
separator = {
column = " | ";
row = "\\n\\n";
};
prompt = {
main = "> ";
continue = "..> ";
};
extraConfig = ''
.help
'';
};

nmt.script =
let
configFile = "home-files/.config/sqlite3/sqliterc";
in
''
assertFileExists "${configFile}"
assertFileContent "${configFile}" ${./basic-configuration-expected}
'';
}
4 changes: 4 additions & 0 deletions tests/modules/programs/sqlite/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
sqlite-basic-configuration = ./basic-configuration.nix;
sqlite-empty-settings = ./empty-settings.nix;
}
7 changes: 7 additions & 0 deletions tests/modules/programs/sqlite/empty-settings.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
programs.sqlite.enable = false;

nmt.script = ''
assertPathNotExists home-files/.config/sqlite
'';
}