Skip to content

Commit

Permalink
feat(home-manager): cava,imv,mako,swaylock sans IFD
Browse files Browse the repository at this point in the history
  • Loading branch information
mightyiam committed Nov 14, 2024
1 parent 32359bf commit 6a3ae62
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 48 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
```
Expand Down
7 changes: 0 additions & 7 deletions docs/src/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
```
Expand Down
2 changes: 1 addition & 1 deletion modules/home-manager/cava.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
};
}
2 changes: 1 addition & 1 deletion modules/home-manager/imv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
);
}
4 changes: 2 additions & 2 deletions modules/home-manager/mako.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion modules/home-manager/swaylock.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
};
}
31 changes: 2 additions & 29 deletions modules/lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions modules/lib/from-ini.nix
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ lib.optionalAttrs nixpkgs.stdenv.isLinux {
home-manager = home-manager-stable;
};
}
// {
from-ini = assert import ./from-ini.nix lib; nixpkgs.hello;
}
33 changes: 33 additions & 0 deletions tests/from-ini.nix
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6a3ae62

Please sign in to comment.