From af2007bb7772c6f17f4924639f2eb8a321d74fb4 Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Tue, 5 Oct 2021 23:05:22 +0200 Subject: [PATCH] atuin: add module This commit adds a module for configuring atuin, a replacement shell history program. The module adds options for generating atuin's `config.toml` from Nix, and options to enable atuin's integration for bash and zsh (which will rebind history keys to open the atuin history). --- .github/CODEOWNERS | 3 + modules/lib/maintainers.nix | 6 ++ modules/misc/news.nix | 7 ++ modules/modules.nix | 1 + modules/programs/atuin.nix | 90 +++++++++++++++++++ tests/default.nix | 1 + tests/modules/programs/atuin/bash.nix | 20 +++++ tests/modules/programs/atuin/default.nix | 7 ++ .../modules/programs/atuin/empty-settings.nix | 14 +++ .../atuin/example-settings-expected.toml | 4 + .../programs/atuin/example-settings.nix | 32 +++++++ tests/modules/programs/atuin/no-shell.nix | 23 +++++ tests/modules/programs/atuin/zsh.nix | 20 +++++ 13 files changed, 228 insertions(+) create mode 100644 modules/programs/atuin.nix create mode 100644 tests/modules/programs/atuin/bash.nix create mode 100644 tests/modules/programs/atuin/default.nix create mode 100644 tests/modules/programs/atuin/empty-settings.nix create mode 100644 tests/modules/programs/atuin/example-settings-expected.toml create mode 100644 tests/modules/programs/atuin/example-settings.nix create mode 100644 tests/modules/programs/atuin/no-shell.nix create mode 100644 tests/modules/programs/atuin/zsh.nix diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index e165a6f22..090d1a18b 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -50,6 +50,9 @@ /modules/programs/autojump.nix @evanjs /tests/modules/programs/autojump @evanjs +/modules/programs/atuin.nix @hawkw +/tests/modules/programs/atuin @hawkw + /modules/programs/autorandr.nix @uvNikita /modules/programs/bash.nix @rycee diff --git a/modules/lib/maintainers.nix b/modules/lib/maintainers.nix index badab65bf..09ea9ca8d 100644 --- a/modules/lib/maintainers.nix +++ b/modules/lib/maintainers.nix @@ -133,4 +133,10 @@ githubId = 1553581; name = "Josh Robson Chase"; }; + hawkw = { + name = "Eliza Weisman"; + email = "eliza@elizas.website"; + github = "hawkw"; + githubId = 2796466; + }; } diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 687e54ce1..68eadae21 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -2214,6 +2214,13 @@ in 'services.screen-locker'. ''; } + + { + time = "2021-10-05T20:55:07+00:00"; + message = '' + A new module is available: 'programs.atuin'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index e87dc85dc..d9667a921 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -45,6 +45,7 @@ let ./programs/alot.nix ./programs/aria2.nix ./programs/astroid.nix + ./programs/atuin.nix ./programs/autojump.nix ./programs/autorandr.nix ./programs/bash.nix diff --git a/modules/programs/atuin.nix b/modules/programs/atuin.nix new file mode 100644 index 000000000..10f503cf4 --- /dev/null +++ b/modules/programs/atuin.nix @@ -0,0 +1,90 @@ +{ config, pkgs, lib, ... }: + +with lib; + +let + + cfg = config.programs.atuin; + + tomlFormat = pkgs.formats.toml { }; + +in { + meta.maintainers = [ maintainers.hawkw ]; + + options.programs.atuin = { + enable = mkEnableOption "atuin"; + + package = mkOption { + type = types.package; + default = pkgs.atuin; + defaultText = literalExample "pkgs.atuin"; + description = "The package to use for atuin."; + }; + + enableBashIntegration = mkOption { + type = types.bool; + default = true; + description = '' + Whether to enable Atuin's Bash integration. This will bind + ctrl-r to open the Atuin history. + ''; + }; + + enableZshIntegration = mkEnableOption "Zsh integration" // { + default = true; + description = '' + Whether to enable Atuin's Zsh integration. + + If enabled, this will bind ctrl-r and the up-arrow + key to open the Atuin history. + ''; + }; + + settings = mkOption { + type = with types; + let + prim = oneOf [ bool int str ]; + primOrPrimAttrs = either prim (attrsOf prim); + entry = either prim (listOf primOrPrimAttrs); + entryOrAttrsOf = t: either entry (attrsOf t); + entries = entryOrAttrsOf (entryOrAttrsOf entry); + in attrsOf entries // { description = "Atuin configuration"; }; + default = { }; + example = literalExample '' + { + auto_sync = true; + sync_frequency = "5m"; + sync_address = "https://api.atuin.sh"; + search_mode = "prefix"; + } + ''; + description = '' + Configuration written to + ~/.config/atuin/config.toml. + + See for the full list + of options. + ''; + }; + }; + + config = mkIf cfg.enable { + + # Always add the configured `atuin` package. + home.packages = [ cfg.package ]; + + # If there are user-provided settings, generate the config file. + xdg.configFile."atuin/config.toml" = mkIf (cfg.settings != { }) { + source = tomlFormat.generate "atuin-config" cfg.settings; + }; + + programs.bash.initExtra = mkIf cfg.enableBashIntegration '' + source "${pkgs.bash-preexec}/share/bash/bash-preexec.sh" + eval "$(${cfg.package}/bin/atuin init bash)" + ''; + + programs.zsh.initExtra = mkIf cfg.enableZshIntegration '' + eval "$(${cfg.package}/bin/atuin init zsh)" + ''; + }; +} diff --git a/tests/default.nix b/tests/default.nix index a65ee7c25..8066433f4 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -46,6 +46,7 @@ import nmt { ./modules/programs/alacritty ./modules/programs/alot ./modules/programs/aria2 + ./modules/programs/atuin ./modules/programs/autojump ./modules/programs/bash ./modules/programs/bat diff --git a/tests/modules/programs/atuin/bash.nix b/tests/modules/programs/atuin/bash.nix new file mode 100644 index 000000000..f8c314099 --- /dev/null +++ b/tests/modules/programs/atuin/bash.nix @@ -0,0 +1,20 @@ +{ ... }: + +{ + programs = { + atuin.enable = true; + bash.enable = true; + }; + + test.stubs = { + atuin = { }; + bash-preexec = { }; + }; + + nmt.script = '' + assertFileExists home-files/.bashrc + assertFileContains \ + home-files/.bashrc \ + 'eval "$(@atuin@/bin/atuin init bash)"' + ''; +} diff --git a/tests/modules/programs/atuin/default.nix b/tests/modules/programs/atuin/default.nix new file mode 100644 index 000000000..386a17288 --- /dev/null +++ b/tests/modules/programs/atuin/default.nix @@ -0,0 +1,7 @@ +{ + atuin-bash = ./bash.nix; + atuin-empty-settings = ./empty-settings.nix; + atuin-example-settings = ./example-settings.nix; + atuin-no-shell = ./no-shell.nix; + atuin-zsh = ./zsh.nix; +} diff --git a/tests/modules/programs/atuin/empty-settings.nix b/tests/modules/programs/atuin/empty-settings.nix new file mode 100644 index 000000000..58f126965 --- /dev/null +++ b/tests/modules/programs/atuin/empty-settings.nix @@ -0,0 +1,14 @@ +{ ... }: + +{ + programs.atuin.enable = true; + + test.stubs = { + atuin = { }; + bash-preexec = { }; + }; + + nmt.script = '' + assertPathNotExists home-files/.config/atuin/config.toml + ''; +} diff --git a/tests/modules/programs/atuin/example-settings-expected.toml b/tests/modules/programs/atuin/example-settings-expected.toml new file mode 100644 index 000000000..c1e92f146 --- /dev/null +++ b/tests/modules/programs/atuin/example-settings-expected.toml @@ -0,0 +1,4 @@ +db_path = "~/.atuin-history.db" +dialect = "us" +auto_sync = true +search-mode = "fulltext" \ No newline at end of file diff --git a/tests/modules/programs/atuin/example-settings.nix b/tests/modules/programs/atuin/example-settings.nix new file mode 100644 index 000000000..f2d3a8876 --- /dev/null +++ b/tests/modules/programs/atuin/example-settings.nix @@ -0,0 +1,32 @@ +{ ... }: + +{ + programs.atuin = { + enable = true; + + settings = { + db_path = "~/.atuin-history.db"; + dialect = "us"; + auto_sync = true; + search-mode = "fulltext"; + }; + }; + + test.stubs = { + atuin = { }; + bash-preexec = { }; + }; + + nmt.script = '' + assertFileContent \ + home-files/.config/atuin/config.toml \ + ${ + builtins.toFile "example-settings-expected.toml" '' + auto_sync = true + db_path = "~/.atuin-history.db" + dialect = "us" + search-mode = "fulltext" + '' + } + ''; +} diff --git a/tests/modules/programs/atuin/no-shell.nix b/tests/modules/programs/atuin/no-shell.nix new file mode 100644 index 000000000..ef3bfe5d6 --- /dev/null +++ b/tests/modules/programs/atuin/no-shell.nix @@ -0,0 +1,23 @@ +{ ... }: + +{ + programs = { + atuin = { + enable = true; + enableBashIntegration = false; + enableZshIntegration = false; + }; + bash.enable = true; + zsh.enable = true; + }; + + test.stubs = { + atuin = { }; + bash-preexec = { }; + }; + + nmt.script = '' + assertFileNotRegex home-files/.zshrc '@atuin@ init zsh' + assertFileNotRegex home-files/.bashrc '@atuin@ init bash' + ''; +} diff --git a/tests/modules/programs/atuin/zsh.nix b/tests/modules/programs/atuin/zsh.nix new file mode 100644 index 000000000..45f0254ca --- /dev/null +++ b/tests/modules/programs/atuin/zsh.nix @@ -0,0 +1,20 @@ +{ ... }: + +{ + programs = { + atuin.enable = true; + zsh.enable = true; + }; + + test.stubs = { + atuin = { }; + bash-preexec = { }; + }; + + nmt.script = '' + assertFileExists home-files/.zshrc + assertFileContains \ + home-files/.zshrc \ + 'eval "$(@atuin@/bin/atuin init zsh)"' + ''; +}