From 58ab3ca245aafff0615f27c6c4c5c0fcac1a51ae Mon Sep 17 00:00:00 2001 From: Alex Lov Date: Thu, 20 Aug 2026 19:35:42 +0000 Subject: [PATCH] feat(wrapperModules.lsd): init --- wrapperModules/l/lsd/check.nix | 96 +++++++++++++++++++++++ wrapperModules/l/lsd/module.nix | 130 ++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 wrapperModules/l/lsd/check.nix create mode 100644 wrapperModules/l/lsd/module.nix diff --git a/wrapperModules/l/lsd/check.nix b/wrapperModules/l/lsd/check.nix new file mode 100644 index 00000000..1dda2336 --- /dev/null +++ b/wrapperModules/l/lsd/check.nix @@ -0,0 +1,96 @@ +{ + pkgs, + self, + tlib, + ... +}: + +let + inherit (tlib) + fileContains + test + ; + wm = self.wrappers.lsd; +in +test { wrapper = "lsd"; } { + "wrapper should output correct version" = + let + wrapper = wm.wrap { + inherit pkgs; + }; + in + ''${wrapper}/bin/lsd --version | grep -q "${wrapper.version}"''; + + "config file contains some settings and it applied" = + let + wrapper = wm.wrap { + inherit pkgs; + settings = { + classic = true; + blocks = [ + "size" + "name" + ]; + size = "bytes"; + }; + }; + in + [ + (fileContains "${wrapper.generatedConfig}config.yaml" "classic") + (fileContains "${wrapper.generatedConfig}config.yaml" "blocks") + (fileContains "${wrapper.generatedConfig}config.yaml" "bytes") + # check the lsd output and that it actually used the config + "${wrapper}/bin/lsd --long ${wrapper.generatedConfig}config.yaml | grep -qE '^48.*config.yaml$'" + ]; + + "custom icons used" = + let + wrapper = wm.wrap { + inherit pkgs; + settings = { + icons.when = "always"; + color.when = "never"; + blocks = [ + "name" + ]; + }; + icons = { + extension = { + "yaml" = ""; + }; + }; + }; + in + [ + (fileContains "${wrapper.generatedConfig}config.yaml" "when: always") + (fileContains "${wrapper.generatedConfig}icons.yaml" "yaml: ") + # check the lsd output and that it actually used the config + "${wrapper}/bin/lsd --long ${wrapper.generatedConfig}icons.yaml | grep -qE '^.*icons.yaml$'" + ]; + "custom color used" = + let + wrapper = wm.wrap { + inherit pkgs; + settings = { + icons.when = "never"; + color.when = "always"; + blocks = [ + "size" + "name" + ]; + size = "bytes"; + }; + colors = { + size = { + small = "cyan"; + }; + }; + }; + in + [ + (fileContains "${wrapper.generatedConfig}config.yaml" "theme: custom") + (fileContains "${wrapper.generatedConfig}colors.yaml" "small: cyan") + # check the lsd output and that it actually used the config + "${wrapper}/bin/lsd --long ${wrapper.generatedConfig}colors.yaml | grep -qE '\\[38;5;14m20.*colors.yaml$'" + ]; +} diff --git a/wrapperModules/l/lsd/module.nix b/wrapperModules/l/lsd/module.nix new file mode 100644 index 00000000..eb783c16 --- /dev/null +++ b/wrapperModules/l/lsd/module.nix @@ -0,0 +1,130 @@ +{ + wlib, + lib, + config, + pkgs, + ... +}: +let + yamlFmtType = wlib.types.structuredValueWith { + nullable = false; + typeName = "YAML"; + }; +in +{ + imports = [ wlib.modules.default ]; + + # Taken from https://github.com/nix-community/home-manager/blob/master/modules/programs/lsd.nix + options = { + settings = lib.mkOption { + type = yamlFmtType; + default = { }; + example = { + date = "relative"; + ignore-globs = [ + ".git" + ".hg" + ]; + }; + description = '' + Configuration written to + {file}`$XDG_CONFIG_HOME/lsd/config.yaml`. See + + for supported values. + ''; + }; + + colors = lib.mkOption { + type = yamlFmtType; + default = { }; + example = { + size = { + none = "grey"; + small = "yellow"; + large = "dark_yellow"; + }; + }; + description = '' + Configuration written to {file}`$XDG_CONFIG_HOME/lsd/colors.yaml`. See + for + supported colors. + + If this option is non-empty then the `settings.color.theme` option is + automatically set to `"custom"`. + ''; + }; + + icons = lib.mkOption { + type = yamlFmtType; + default = { }; + example = { + name = { + ".trash" = ""; + ".cargo" = ""; + }; + extension = { + "go" = ""; + "hs" = ""; + }; + filetype = { + "dir" = "📂"; + "file" = "📄"; + }; + }; + description = '' + Configuration written to {file}`$XDG_CONFIG_HOME/lsd/icons.yaml`. See + for + details. + ''; + }; + + generatedConfig.output = lib.mkOption { + type = lib.types.str; + default = config.outputName; + description = '' + The derivation output for the config generated by this wrapper module + ''; + }; + generatedConfig.placeholder = lib.mkOption { + type = lib.types.str; + default = "${placeholder config.generatedConfig.output}/config/${config.binName}"; + readOnly = true; + description = '' + The placeholder accessible in the wrapper derivation build script for the config generated by this wrapper module + ''; + }; + }; + + config = { + package = lib.mkDefault pkgs.lsd; + # lsd looks for icons.yaml and colors.yaml files strictly under "$XDG_CONFIG_HOME/lsd", thus hardcoding this env var + env.XDG_CONFIG_HOME = "${placeholder "out"}/config/"; + + settings.color = lib.mkIf (config.colors != { }) { theme = "custom"; }; + + # Inspired by https://github.com/BirdeeHub/nix-wrapper-modules/blob/main/wrapperModules/y/yazi/module.nix + constructFiles = + builtins.mapAttrs + (n: v: { + # generate the directory of toml files + # use lib.mkOverride 0 to force the value to make sure they stay together + relPath = lib.mkOverride 0 "config/${config.binName}/${n}.yaml"; + output = lib.mkOverride 0 config.generatedConfig.output; + content = builtins.toJSON v; + # redefine builder to convert the result to yaml. + # This is how pkgs.format.yaml works (more or less) + builder = ''${pkgs.remarshal}/bin/json2yaml "$1" "$2"''; + }) + { + inherit (config) colors icons; + "config" = config.settings; + }; + + # make `finalpackage.generatedConfig` point to the generated config so it can be used from outside of the wrapper module + passthru.generatedConfig = "${ + config.wrapper.${config.generatedConfig.output} + }/config/${config.binName}/"; + + meta.maintainers = [ wlib.maintainers.alexlov ]; + }; +}