From b365342adb36fd659a1472dced6f55b706c15162 Mon Sep 17 00:00:00 2001 From: Mario Rodas Date: Thu, 11 May 2023 04:20:49 -0500 Subject: [PATCH] ledger: add structural `settings` option (#3661) --- .github/CODEOWNERS | 1 + modules/programs/ledger.nix | 38 ++++++++++++++++++++--- tests/default.nix | 1 + tests/modules/programs/ledger/default.nix | 1 + tests/modules/programs/ledger/ledger.nix | 35 +++++++++++++++++++++ 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 tests/modules/programs/ledger/default.nix create mode 100644 tests/modules/programs/ledger/ledger.nix diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 6d50bbab..9d12e96f 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -180,6 +180,7 @@ Makefile @thiagokokada /modules/programs/lazygit.nix @kalhauge /modules/programs/ledger.nix @marsam +/tests/modules/programs/ledger @marsam /modules/programs/less.nix @pamplemousse /tests/modules/programs/less @pamplemousse diff --git a/modules/programs/ledger.nix b/modules/programs/ledger.nix index c51ed7d1..ae9fb368 100644 --- a/modules/programs/ledger.nix +++ b/modules/programs/ledger.nix @@ -6,6 +6,15 @@ let cfg = config.programs.ledger; + cfgText = generators.toKeyValue { + mkKeyValue = key: value: + if isBool value then + optionalString value "--${key}" + else + "--${key} ${toString value}"; + listsAsDuplicateKeys = true; + } cfg.settings; + in { meta.maintainers = [ maintainers.marsam ]; @@ -14,6 +23,26 @@ in { package = mkPackageOption pkgs "ledger" { }; + settings = mkOption { + type = with types; attrsOf (oneOf [ bool int str (listOf str) ]); + default = { }; + example = { + sort = "date"; + date-format = "%Y-%m-%d"; + strict = true; + file = [ + "~/finances/journal.ledger" + "~/finances/assets.ledger" + "~/finances/income.ledger" + ]; + }; + description = '' + Configuration written to $XDG_CONFIG_HOME/ledger/ledgerrc. + See + for explanation about possible values. + ''; + }; + extraConfig = mkOption { type = types.lines; default = ""; @@ -23,9 +52,8 @@ in { --date-format %Y-%m-%d ''; description = '' - Configuration written to $XDG_CONFIG_HOME/ledger/ledgerrc. - See - for explanation about possible values. + Extra configuration to add to + $XDG_CONFIG_HOME/ledger/ledgerrc. ''; }; }; @@ -34,6 +62,8 @@ in { home.packages = [ cfg.package ]; xdg.configFile."ledger/ledgerrc" = - mkIf (cfg.extraConfig != "") { text = cfg.extraConfig; }; + mkIf (cfg.settings != { } || cfg.extraConfig != "") { + text = cfgText + cfg.extraConfig; + }; }; } diff --git a/tests/default.nix b/tests/default.nix index f2e20d24..c4e009eb 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -86,6 +86,7 @@ import nmt { ./modules/programs/k9s ./modules/programs/kakoune ./modules/programs/kitty + ./modules/programs/ledger ./modules/programs/less ./modules/programs/lf ./modules/programs/lieer diff --git a/tests/modules/programs/ledger/default.nix b/tests/modules/programs/ledger/default.nix new file mode 100644 index 00000000..d7556a0b --- /dev/null +++ b/tests/modules/programs/ledger/default.nix @@ -0,0 +1 @@ +{ ledger = ./ledger.nix; } diff --git a/tests/modules/programs/ledger/ledger.nix b/tests/modules/programs/ledger/ledger.nix new file mode 100644 index 00000000..f120a654 --- /dev/null +++ b/tests/modules/programs/ledger/ledger.nix @@ -0,0 +1,35 @@ +{ ... }: + +{ + programs.ledger = { + enable = true; + settings = { + sort = "date"; + strict = true; + pedantic = true; + leeway = 30; + date-format = "%Y-%m-%d"; + file = [ + "~/finances/journal.ledger" + "~/finances/assets.ledger" + "~/finances/income.ledger" + ]; + }; + }; + + test.stubs.ledger = { }; + + nmt.script = '' + assertFileContent home-files/.config/ledger/ledgerrc \ + ${builtins.toFile "ledger-expected-settings" '' + --date-format %Y-%m-%d + --file ~/finances/journal.ledger + --file ~/finances/assets.ledger + --file ~/finances/income.ledger + --leeway 30 + --pedantic + --sort date + --strict + ''} + ''; +}