From e71e678d18d1a24e01d823ccb72df13f9e82f65b Mon Sep 17 00:00:00 2001 From: Terje Larsen Date: Thu, 18 Jul 2024 14:15:10 +0200 Subject: [PATCH] nix-your-shell: add module --- modules/misc/news.nix | 10 ++++ modules/modules.nix | 1 + modules/programs/nix-your-shell.nix | 55 +++++++++++++++++++ tests/default.nix | 1 + .../programs/nix-your-shell/default.nix | 1 + .../programs/nix-your-shell/enable-shells.nix | 48 ++++++++++++++++ 6 files changed, 116 insertions(+) create mode 100644 modules/programs/nix-your-shell.nix create mode 100644 tests/modules/programs/nix-your-shell/default.nix create mode 100644 tests/modules/programs/nix-your-shell/enable-shells.nix diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 32394f335..75990f087 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1827,6 +1827,16 @@ in { as systemd services. ''; } + + { + time = "2024-12-01T19:17:40+00:00"; + message = '' + A new module is available: 'programs.nix-your-shell'. + + nix-your-shell is a wrapper for `nix develop` or `nix-shell` to retain + the same shell inside the new environment. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 75f8ac461..7d23c950a 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -176,6 +176,7 @@ let ./programs/nh.nix ./programs/nheko.nix ./programs/nix-index.nix + ./programs/nix-your-shell.nix ./programs/nnn.nix ./programs/noti.nix ./programs/notmuch.nix diff --git a/modules/programs/nix-your-shell.nix b/modules/programs/nix-your-shell.nix new file mode 100644 index 000000000..5e19e8801 --- /dev/null +++ b/modules/programs/nix-your-shell.nix @@ -0,0 +1,55 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.programs.nix-your-shell; + +in { + meta.maintainers = [ maintainers.terlar ]; + + options.programs.nix-your-shell = { + enable = mkEnableOption '' + {command}`nix-your-shell`, a wrapper for `nix develop` or `nix-shell` + to retain the same shell inside the new environment''; + + package = mkPackageOption pkgs "nix-your-shell" { }; + + enableFishIntegration = mkEnableOption "Fish integration" // { + default = true; + }; + + enableNushellIntegration = mkEnableOption "Nushell integration" // { + default = true; + }; + + enableZshIntegration = mkEnableOption "Zsh integration" // { + default = true; + }; + }; + + config = mkIf cfg.enable { + home.packages = [ cfg.package ]; + + programs = { + fish.interactiveShellInit = mkIf cfg.enableFishIntegration '' + ${cfg.package}/bin/nix-your-shell fish | source + ''; + + nushell = mkIf cfg.enableNushellIntegration { + extraEnv = '' + ${cfg.package}/bin/nix-your-shell nu | save --force ${config.xdg.cacheHome}/nix-your-shell/init.nu + ''; + + extraConfig = '' + source ${config.xdg.cacheHome}/nix-your-shell/init.nu + ''; + }; + + zsh.initExtra = mkIf cfg.enableZshIntegration '' + ${cfg.package}/bin/nix-your-shell zsh | source /dev/stdin + ''; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 7ddddc166..ec381414c 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -118,6 +118,7 @@ in import nmtSrc { ./modules/programs/newsboat ./modules/programs/nheko ./modules/programs/nix-index + ./modules/programs/nix-your-shell ./modules/programs/nnn ./modules/programs/nushell ./modules/programs/oh-my-posh diff --git a/tests/modules/programs/nix-your-shell/default.nix b/tests/modules/programs/nix-your-shell/default.nix new file mode 100644 index 000000000..06d2f688c --- /dev/null +++ b/tests/modules/programs/nix-your-shell/default.nix @@ -0,0 +1 @@ +{ nix-your-shell-enable-shells = ./enable-shells.nix; } diff --git a/tests/modules/programs/nix-your-shell/enable-shells.nix b/tests/modules/programs/nix-your-shell/enable-shells.nix new file mode 100644 index 000000000..ab8c6909e --- /dev/null +++ b/tests/modules/programs/nix-your-shell/enable-shells.nix @@ -0,0 +1,48 @@ +{ pkgs, config, ... }: + +{ + programs = { + nix-your-shell = { + enable = true; + enableFishIntegration = true; + enableNushellIntegration = true; + enableZshIntegration = true; + }; + fish.enable = true; + nushell.enable = true; + zsh.enable = true; + }; + + test.stubs = { + nix-your-shell = { }; + nushell = { }; + zsh = { }; + }; + + nmt.script = let + nushellConfigDir = if pkgs.stdenv.isDarwin && !config.xdg.enable then + "home-files/Library/Application Support/nushell" + else + "home-files/.config/nushell"; + in '' + assertFileExists home-files/.config/fish/config.fish + assertFileContains \ + home-files/.config/fish/config.fish \ + '@nix-your-shell@/bin/nix-your-shell fish | source' + + assertFileExists ${nushellConfigDir}/config.nu + assertFileContains \ + ${nushellConfigDir}/config.nu \ + 'source ${config.xdg.cacheHome}/nix-your-shell/init.nu' + + assertFileExists ${nushellConfigDir}/env.nu + assertFileContains \ + ${nushellConfigDir}/env.nu \ + '@nix-your-shell@/bin/nix-your-shell nu | save --force ${config.xdg.cacheHome}/nix-your-shell/init.nu' + + assertFileExists home-files/.zshrc + assertFileContains \ + home-files/.zshrc \ + '@nix-your-shell@/bin/nix-your-shell zsh | source /dev/stdin' + ''; +}