Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions wrapperModules/l/lsd/check.nix
Original file line number Diff line number Diff line change
@@ -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$'"
];
}
130 changes: 130 additions & 0 deletions wrapperModules/l/lsd/module.nix
Original file line number Diff line number Diff line change
@@ -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
<https://github.com/Peltoche/lsd#config-file-content>
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
<https://github.com/lsd-rs/lsd/tree/v1.0.0#color-theme-file-content> 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
<https://github.com/lsd-rs/lsd?tab=readme-ov-file#icon-theme-file-content> 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 ];
};
}