Skip to content

Commit 0f98c61

Browse files
arunorutotrueNAHO
andcommitted
vivid: running at evaluation time
Co-authored-by: NAHO <[email protected]>
1 parent e121f37 commit 0f98c61

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed

modules/programs/vivid.nix

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,51 @@
77
let
88
inherit (lib)
99
mkIf
10+
mkMerge
1011
mkEnableOption
1112
mkPackageOption
1213
mkOption
1314
types
1415
;
1516

16-
inherit (lib.hm.shell)
17-
mkBashIntegrationOption
18-
mkZshIntegrationOption
19-
mkFishIntegrationOption
20-
;
21-
2217
cfg = config.programs.vivid;
2318
yamlFormat = pkgs.formats.yaml { };
2419
in
2520
{
26-
meta.maintainers = with lib.hm.maintainers; [ aguirre-matteo ];
21+
meta.maintainers = [
22+
lib.hm.maintainers.aguirre-matteo
23+
lib.maintainers.arunoruto
24+
];
25+
26+
imports = [
27+
(lib.mkRemovedOptionModule [
28+
"programs"
29+
"vivid"
30+
"enableBashIntegration"
31+
] "Now the shell integration is always enabled.")
32+
(lib.mkRemovedOptionModule [
33+
"programs"
34+
"vivid"
35+
"enableZshIntegration"
36+
] "Now the shell integration is always enabled.")
37+
(lib.mkRemovedOptionModule [
38+
"programs"
39+
"vivid"
40+
"enableFishIntegration"
41+
] "Now the shell integration is always enabled.")
42+
];
2743

2844
options.programs.vivid = {
2945
enable = mkEnableOption "vivid";
3046
package = mkPackageOption pkgs "vivid" { nullable = true; };
3147

32-
enableBashIntegration = mkBashIntegrationOption { inherit config; };
33-
enableZshIntegration = mkZshIntegrationOption { inherit config; };
34-
enableFishIntegration = mkFishIntegrationOption { inherit config; };
35-
3648
colorMode = mkOption {
37-
type = with types; nullOr str;
49+
type =
50+
with types;
51+
nullOr (enum [
52+
"8-bit"
53+
"24-bit"
54+
]);
3855
default = null;
3956
example = "8-bit";
4057
description = ''
@@ -71,8 +88,8 @@ in
7188
};
7289

7390
activeTheme = mkOption {
74-
type = with types; nullOr str;
75-
default = null;
91+
type = types.str;
92+
default = "";
7693
example = "molokai";
7794
description = ''
7895
Active theme for vivid.
@@ -116,16 +133,30 @@ in
116133

117134
};
118135

119-
config =
120-
let
121-
vividCommand = "vivid ${
122-
lib.optionalString (cfg.colorMode != null) "-m ${cfg.colorMode}"
123-
} generate ${lib.optionalString (cfg.activeTheme != null) cfg.activeTheme}";
124-
in
125-
mkIf cfg.enable {
126-
home.packages = mkIf (cfg.package != null) [ cfg.package ];
127-
128-
home.sessionVariables = mkIf (cfg.activeTheme != null) { VIVID_THEME = cfg.activeTheme; };
136+
config = mkMerge [
137+
(mkIf (cfg.enable || cfg.themes != { }) {
138+
home.sessionVariables.LS_COLORS =
139+
let
140+
colorMode = lib.optionalString (cfg.colorMode != null) "-m ${cfg.colorMode}";
141+
filetypes = lib.optionalString (
142+
cfg.filetypes != { }
143+
) "-d ${yamlFormat.generate "vivid-filetypes" cfg.filetypes}";
144+
themePath =
145+
if builtins.isAttrs cfg.themes.${cfg.activeTheme} then
146+
pkgs.writeText "${cfg.activeTheme}.json" (builtins.toJSON cfg.themes.${cfg.activeTheme})
147+
else if config.xdg.configFile ? "vivid/themes/${cfg.activeTheme}.yml" then
148+
config.xdg.configFile."vivid/themes/${cfg.activeTheme}.yml".source
149+
else
150+
cfg.activeTheme;
151+
in
152+
"$(cat ${
153+
pkgs.runCommand "ls-colors" {
154+
nativeBuildInputs = [ cfg.package ];
155+
} "vivid ${colorMode} ${filetypes} generate ${themePath} >$out"
156+
})";
157+
})
158+
(mkIf cfg.enable {
159+
home.sessionVariables = mkIf (cfg.activeTheme != "") { VIVID_THEME = cfg.activeTheme; };
129160

130161
xdg.configFile = {
131162
"vivid/filetypes.yml" = mkIf (cfg.filetypes != { }) {
@@ -135,20 +166,12 @@ in
135166
// (lib.mapAttrs' (
136167
name: value:
137168
lib.nameValuePair "vivid/themes/${name}.yml" {
138-
source = if lib.isAttrs value then yamlFormat.generate "${name}.yml" value else value;
169+
# Since JSON is a YAML subset, it can be used to populate the config file
170+
# JSON has better support in Nix than YAML
171+
# Values like 1e2030 will be treated as a string by Nix, but in YAML are read as a number in scientific notation
172+
source = if lib.isAttrs value then pkgs.writeText "${name}.json" (builtins.toJSON value) else value;
139173
}
140174
) cfg.themes);
141-
142-
programs.bash.initExtra = mkIf cfg.enableBashIntegration ''
143-
export LS_COLORS="$(${vividCommand})"
144-
'';
145-
146-
programs.zsh.initContent = mkIf cfg.enableZshIntegration ''
147-
export LS_COLORS="$(${vividCommand})"
148-
'';
149-
150-
programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration ''
151-
set -gx LS_COLORS "$(${vividCommand})"
152-
'';
153-
};
175+
})
176+
];
154177
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
colors:
2-
primary: 00aaff
3-
secondary: ff00aa
4-
core:
5-
directory:
6-
foreground: primary
7-
executable-file:
8-
font-style: bold
9-
foreground: secondary
1+
{"colors":{"primary":"00aaff","secondary":"ff00aa"},"core":{"directory":{"foreground":"primary"},"executable-file":{"font-style":"bold","foreground":"secondary"}}}

0 commit comments

Comments
 (0)