From 6a3ae62fb3fb596223bb5ddc6b3dc1b11266155a Mon Sep 17 00:00:00 2001 From: "Shahar \"Dawn\" Or" Date: Thu, 14 Nov 2024 11:40:50 +0700 Subject: [PATCH] feat(home-manager): cava,imv,mako,swaylock sans IFD Closes #340 Closes #341 Closes #375 Closes #376 Closes #373 --- README.md | 7 ---- docs/src/faq.md | 7 ---- modules/home-manager/cava.nix | 2 +- modules/home-manager/imv.nix | 2 +- modules/home-manager/mako.nix | 4 +- modules/home-manager/swaylock.nix | 2 +- modules/lib/default.nix | 31 +--------------- modules/lib/from-ini.nix | 62 +++++++++++++++++++++++++++++++ tests/default.nix | 3 ++ tests/from-ini.nix | 33 ++++++++++++++++ 10 files changed, 105 insertions(+), 48 deletions(-) create mode 100644 modules/lib/from-ini.nix create mode 100644 tests/from-ini.nix diff --git a/README.md b/README.md index 2dbb0d49..d6ec6127 100644 --- a/README.md +++ b/README.md @@ -182,14 +182,7 @@ For [standalone installations](https://nix-community.github.io/home-manager/inde ```nix { programs = { - cava.catppuccin.enable = false; gh-dash.catppuccin.enable = false; - imv.catppuccin.enable = false; - swaylock.catppuccin.enable = false; - }; - - services = { - mako.catppuccin.enable = false; }; } ``` diff --git a/docs/src/faq.md b/docs/src/faq.md index 8f098491..d30206ee 100644 --- a/docs/src/faq.md +++ b/docs/src/faq.md @@ -20,14 +20,7 @@ ```nix { programs = { - cava.catppuccin.enable = false; gh-dash.catppuccin.enable = false; - imv.catppuccin.enable = false; - swaylock.catppuccin.enable = false; - }; - - services = { - mako.catppuccin.enable = false; }; } ``` diff --git a/modules/home-manager/cava.nix b/modules/home-manager/cava.nix index 80d63fe8..8acd8434 100644 --- a/modules/home-manager/cava.nix +++ b/modules/home-manager/cava.nix @@ -11,6 +11,6 @@ in }; config.programs.cava = lib.mkIf enable { - settings = lib.ctp.fromINIRaw (sources.cava + "/themes/${flavor}.cava"); + settings = lib.ctp.fromINI (builtins.readFile (sources.cava + "/themes/${flavor}.cava")); }; } diff --git a/modules/home-manager/imv.nix b/modules/home-manager/imv.nix index 515b954c..a987d6e8 100644 --- a/modules/home-manager/imv.nix +++ b/modules/home-manager/imv.nix @@ -8,6 +8,6 @@ in options.programs.imv.catppuccin = lib.ctp.mkCatppuccinOpt { name = "imv"; }; config.programs.imv.settings = lib.mkIf enable ( - lib.ctp.fromINI (sources.imv + "/themes/${cfg.flavor}.config") + lib.ctp.fromINI (builtins.readFile (sources.imv + "/themes/${cfg.flavor}.config")) ); } diff --git a/modules/home-manager/mako.nix b/modules/home-manager/mako.nix index 68ec305a..8fde4275 100644 --- a/modules/home-manager/mako.nix +++ b/modules/home-manager/mako.nix @@ -8,9 +8,9 @@ let inherit (config.catppuccin) sources; cfg = config.services.mako.catppuccin; enable = cfg.enable && config.services.mako.enable; - theme = lib.ctp.fromINI ( + theme = lib.ctp.fromINI (builtins.readFile ( sources.mako + "/themes/catppuccin-${cfg.flavor}/catppuccin-${cfg.flavor}-${cfg.accent}" - ); + )); # Settings that need to be extracted and put in extraConfig extraConfigAttrs = lib.attrsets.getAttrs [ "urgency=high" ] theme; diff --git a/modules/home-manager/swaylock.nix b/modules/home-manager/swaylock.nix index 93e490bb..b299f69d 100644 --- a/modules/home-manager/swaylock.nix +++ b/modules/home-manager/swaylock.nix @@ -34,6 +34,6 @@ in }; config = lib.mkIf cfg.enable { - programs.swaylock.settings = lib.ctp.fromINI (sources.swaylock + "/themes/${cfg.flavor}.conf"); + programs.swaylock.settings = lib.ctp.fromINI (builtins.readFile (sources.swaylock + "/themes/${cfg.flavor}.conf")); }; } diff --git a/modules/lib/default.nix b/modules/lib/default.nix index ec446745..c2d565db 100644 --- a/modules/lib/default.nix +++ b/modules/lib/default.nix @@ -75,35 +75,8 @@ in in builtins.fromJSON (builtins.readFile json); - # a -> path -> a - # fromJSON but for ini (and without readFile) - # a should be the local pkgs attrset - fromINI = - file: - let - # convert to json - json = pkgs.runCommand "converted.json" { } '' - ${lib.getExe pkgs.jc} --ini < ${file} > $out - ''; - in - builtins.fromJSON (builtins.readFile json); - - # a -> path -> a - # fromJSON but for raw ini (and without readFile) - # a should be the local pkgs attrset - fromINIRaw = - file: - let - inherit (builtins) fromJSON readFile; - - # convert to json - json = - with pkgs; - runCommand "converted.json" { } '' - ${jc}/bin/jc --ini -r < ${file} > $out - ''; - in - fromJSON (readFile json); + # string -> a + fromINI = import ./from-ini.nix lib; # string -> a # this creates a basic attrset only containing an diff --git a/modules/lib/from-ini.nix b/modules/lib/from-ini.nix new file mode 100644 index 00000000..a52590d4 --- /dev/null +++ b/modules/lib/from-ini.nix @@ -0,0 +1,62 @@ +# Simplifying assumptions: +# +# - ASCII +# - line feed separated lines +# - no multiline elements +# - `=` surrounded by single space on both sides or not +# - no subsections +# - supported comment forms: +# - line that starts with `#` +# - values are either not quoted or single quoted +# - blank lines are supported +# - otherwise no extra whitespace +lib: ini: +let + lines = lib.strings.splitString "\n" ini; + parsedLines = map parseLine lines; + + parseLine = line: + if builtins.stringLength line == 0 + then { type = "empty"; } + else if lib.hasPrefix "#" line + then { type = "comment"; } + else if lib.hasPrefix "[" line + then { + type = "section"; + name = lib.pipe line [(lib.removePrefix "[") (lib.removeSuffix "]")]; + } + else let + parts = lib.splitString "=" line; + key = lib.removeSuffix " " (lib.elemAt parts 0); + litVal = lib.removePrefix " " (lib.elemAt parts 1); + in { + type = "property"; + inherit key; + val = lib.pipe litVal [(lib.removePrefix "'") (lib.removeSuffix "'")]; + } + ; + + endState = lib.foldl foldState { val = {}; currentSection = null; } parsedLines; + + foldState = acc: line: + if line.type == "empty" + then acc + else if line.type == "comment" + then acc + else if line.type == "section" + then acc // { ${line.name} = {}; currentSection = line.name; } + else if line.type == "property" + then lib.updateManyAttrsByPath + [{ + path = lib.flatten [ + "val" + (lib.optional (builtins.isString acc.currentSection) acc.currentSection) + line.key + ]; + update = _: line.val; + }] + acc + else throw "no such type ${line.type}" + ; +in + endState.val diff --git a/tests/default.nix b/tests/default.nix index cc370e7a..830934da 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -15,3 +15,6 @@ lib.optionalAttrs nixpkgs.stdenv.isLinux { home-manager = home-manager-stable; }; } +// { + from-ini = assert import ./from-ini.nix lib; nixpkgs.hello; +} diff --git a/tests/from-ini.nix b/tests/from-ini.nix new file mode 100644 index 00000000..d07d9a85 --- /dev/null +++ b/tests/from-ini.nix @@ -0,0 +1,33 @@ +lib: +let + subject = import ../modules/lib/from-ini.nix lib; + + input = '' + k=v + + [a] + k=v + #comment + kk='vv' + + [aa] + k = v + #comment + kk = 'vv' + ''; + + expected = { + k = "v"; + a = { + k = "v"; + kk = "vv"; + }; + aa = { + k = "v"; + kk = "vv"; + }; + }; + + actual = subject input; +in + actual == expected