From 08a778d80308353f4f65c9dcd3790b5da02d6306 Mon Sep 17 00:00:00 2001 From: Mario Rodas Date: Fri, 27 Jan 2023 04:20:00 +0000 Subject: [PATCH] papis: add module --- .github/CODEOWNERS | 3 + modules/misc/news.nix | 7 ++ modules/modules.nix | 1 + modules/programs/papis.nix | 91 ++++++++++++++++++++++++ tests/default.nix | 1 + tests/modules/programs/papis/default.nix | 1 + tests/modules/programs/papis/papis.nix | 46 ++++++++++++ 7 files changed, 150 insertions(+) create mode 100644 modules/programs/papis.nix create mode 100644 tests/modules/programs/papis/default.nix create mode 100644 tests/modules/programs/papis/papis.nix diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 9ade1106..e2668548 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -249,6 +249,9 @@ Makefile @thiagokokada /modules/programs/pandoc.nix @kirelagin /tests/modules/programs/pandoc @kirelagin +/modules/programs/papis.nix @marsam +/tests/modules/programs/papis @marsam + /modules/programs/password-store.nix @pacien /modules/programs/pazi.nix @marsam diff --git a/modules/misc/news.nix b/modules/misc/news.nix index f59d50b5..6626e109 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -886,6 +886,13 @@ in 'wayland.windowManager.sway.config.[window|floating].titlebar' now default to 'true'. ''; } + + { + time = "2023-01-28T17:35:49+00:00"; + message = '' + A new module is available: 'programs.papis'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 7e6d2e71..f831bcac 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -144,6 +144,7 @@ let ./programs/oh-my-posh.nix ./programs/opam.nix ./programs/pandoc.nix + ./programs/papis.nix ./programs/password-store.nix ./programs/pazi.nix ./programs/pet.nix diff --git a/modules/programs/papis.nix b/modules/programs/papis.nix new file mode 100644 index 00000000..8e127e26 --- /dev/null +++ b/modules/programs/papis.nix @@ -0,0 +1,91 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.programs.papis; + + defaultLibraries = remove null + (mapAttrsToList (n: v: if v.isDefault then n else null) cfg.libraries); + + settingsIni = (lib.mapAttrs (n: v: v.settings) cfg.libraries) // { + settings = cfg.settings // { "default-library" = head defaultLibraries; }; + }; + +in { + meta.maintainers = [ maintainers.marsam ]; + + options.programs.papis = { + enable = mkEnableOption "papis"; + + settings = mkOption { + type = with types; attrsOf (oneOf [ bool int str ]); + default = { }; + example = literalExpression '' + { + editor = "nvim"; + file-browser = "ranger" + add-edit = true; + } + ''; + description = '' + Configuration written to + $XDG_CONFIG_HOME/papis/config. See + + for supported values. + ''; + }; + + libraries = mkOption { + type = types.attrsOf (types.submodule ({ config, name, ... }: { + options = { + name = mkOption { + type = types.str; + default = name; + readOnly = true; + description = "This library's name."; + }; + + isDefault = mkOption { + type = types.bool; + default = false; + example = true; + description = '' + Whether this is a default library. There must be exactly one + default library. + ''; + }; + + settings = mkOption { + type = with types; attrsOf (oneOf [ bool int str ]); + default = { }; + example = literalExpression '' + { + dir = "~/papers/"; + } + ''; + description = '' + Configuration for this library. + ''; + }; + }; + })); + }; + }; + + config = mkIf cfg.enable { + assertions = [{ + assertion = cfg.libraries == { } || length defaultLibraries == 1; + message = "Must have exactly one default papis library, but found " + + toString (length defaultLibraries) + + optionalString (length defaultLibraries > 1) + (", namely " + concatStringsSep "," defaultLibraries); + }]; + + home.packages = [ pkgs.papis ]; + + xdg.configFile."papis/config" = + mkIf (cfg.libraries != { }) { text = generators.toINI { } settingsIni; }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index ac31f3b5..9c60acea 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -102,6 +102,7 @@ import nmt { ./modules/programs/nushell ./modules/programs/oh-my-posh ./modules/programs/pandoc + ./modules/programs/papis ./modules/programs/pet ./modules/programs/pistol ./modules/programs/pls diff --git a/tests/modules/programs/papis/default.nix b/tests/modules/programs/papis/default.nix new file mode 100644 index 00000000..a849be85 --- /dev/null +++ b/tests/modules/programs/papis/default.nix @@ -0,0 +1 @@ +{ papis = ./papis.nix; } diff --git a/tests/modules/programs/papis/papis.nix b/tests/modules/programs/papis/papis.nix new file mode 100644 index 00000000..507056be --- /dev/null +++ b/tests/modules/programs/papis/papis.nix @@ -0,0 +1,46 @@ +{ ... }: + +{ + programs.papis = { + enable = true; + settings = { + picktool = "fzf"; + file-browser = "ranger"; + add-edit = true; + }; + libraries = { + papers = { + isDefault = true; + settings = { + dir = "~/papers"; + opentool = "okular"; + }; + }; + books.settings = { + dir = "~/books"; + opentool = "firefox"; + }; + }; + }; + + test.stubs.papis = { }; + + nmt.script = '' + assertFileContent home-files/.config/papis/config \ + ${builtins.toFile "papis-expected-settings.ini" '' + [books] + dir=~/books + opentool=firefox + + [papers] + dir=~/papers + opentool=okular + + [settings] + add-edit=true + default-library=papers + file-browser=ranger + picktool=fzf + ''} + ''; +}