1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

carapace: build-time nushell config generation

This commit is contained in:
jaredmontoya 2025-03-21 20:15:27 +01:00 committed by Austin Horstman
parent 3722855a1c
commit dde05a0b10
2 changed files with 22 additions and 32 deletions

View file

@ -1,20 +1,16 @@
{ config, pkgs, lib, ... }:
let
inherit (lib)
mkEnableOption mkPackageOption mkIf pipe fileContents splitString;
cfg = config.programs.carapace;
bin = cfg.package + "/bin/carapace";
bin = lib.getExe cfg.package;
in {
meta.maintainers = with lib.maintainers; [ weathercold bobvanderlinden ];
options.programs.carapace = {
enable =
mkEnableOption "carapace, a multi-shell multi-command argument completer";
enable = lib.mkEnableOption
"carapace, a multi-shell multi-command argument completer";
package = mkPackageOption pkgs "carapace" { };
package = lib.mkPackageOption pkgs "carapace" { };
enableBashIntegration =
lib.hm.shell.mkBashIntegrationOption { inherit config; };
@ -29,39 +25,34 @@ in {
lib.hm.shell.mkZshIntegrationOption { inherit config; };
};
config = mkIf cfg.enable {
config = lib.mkIf cfg.enable {
home.packages = [ cfg.package ];
programs = {
bash.initExtra = mkIf cfg.enableBashIntegration ''
bash.initExtra = lib.mkIf cfg.enableBashIntegration ''
source <(${bin} _carapace bash)
'';
zsh.initContent = mkIf cfg.enableZshIntegration ''
zsh.initContent = lib.mkIf cfg.enableZshIntegration ''
source <(${bin} _carapace zsh)
'';
fish.interactiveShellInit = mkIf cfg.enableFishIntegration ''
fish.interactiveShellInit = lib.mkIf cfg.enableFishIntegration ''
${bin} _carapace fish | source
'';
nushell = mkIf cfg.enableNushellIntegration {
# Note, the ${"$"} below is a work-around because xgettext otherwise
# interpret it as a Bash i18n string.
extraEnv = ''
let carapace_cache = "${config.xdg.cacheHome}/carapace"
if not ($carapace_cache | path exists) {
mkdir $carapace_cache
}
${bin} _carapace nushell | save -f ${"$"}"($carapace_cache)/init.nu"
'';
nushell = lib.mkIf cfg.enableNushellIntegration {
extraConfig = ''
source ${config.xdg.cacheHome}/carapace/init.nu
source ${
pkgs.runCommand "carapace-nushell-config" { } ''
${bin} _carapace nushell >> "$out"
''
}
'';
};
};
xdg.configFile = mkIf (config.programs.fish.enable
xdg.configFile = lib.mkIf (config.programs.fish.enable
&& cfg.enableFishIntegration
&& lib.versionOlder config.programs.fish.package.version "4.0.0") (
# Convert the entries from `carapace --list` to empty
@ -84,9 +75,9 @@ in {
} ''
${bin} --list > $out
'';
in pipe carapaceListFile [
fileContents
(splitString "\n")
in lib.pipe carapaceListFile [
lib.fileContents
(lib.splitString "\n")
(map (builtins.match "^([a-z0-9-]+) .*"))
(builtins.filter
(match: match != null && (builtins.length match) > 0))

View file

@ -1,4 +1,4 @@
{ pkgs, config, ... }:
{ lib, pkgs, realPkgs, config, ... }:
{
programs = {
@ -6,17 +6,16 @@
nushell.enable = true;
};
_module.args.pkgs = lib.mkForce realPkgs;
nmt.script = let
configDir = if pkgs.stdenv.isDarwin && !config.xdg.enable then
"home-files/Library/Application Support/nushell"
else
"home-files/.config/nushell";
in ''
assertFileExists "${configDir}/env.nu"
assertFileRegex "${configDir}/env.nu" \
'/nix/store/.*carapace.*/bin/carapace _carapace nushell \| save -f \$"(\$carapace_cache)/init\.nu"'
assertFileExists "${configDir}/config.nu"
assertFileRegex "${configDir}/config.nu" \
'source /.*/\.cache/carapace/init\.nu'
'source /nix/store/[^/]*-carapace-nushell-config'
'';
}