diff --git a/modules/darwin/options.nix b/modules/darwin/options.nix index 31ab85c..58c8d9c 100644 --- a/modules/darwin/options.nix +++ b/modules/darwin/options.nix @@ -94,10 +94,6 @@ in { withNotify = true; }) ]; - environment = { - profiles = mkOrder 800 ["${config.home.stateDir}/nix/profile"]; - variables = config.modules.xdg.value; - }; time.timeZone = mkDefault my.timezone; modules = { shell = { diff --git a/modules/darwin/shell.nix b/modules/darwin/shell.nix new file mode 100644 index 0000000..e292e53 --- /dev/null +++ b/modules/darwin/shell.nix @@ -0,0 +1,79 @@ +{ + pkgs, + lib, + config, + my, + ... +}: +with lib; +with my; let + env-paths = pkgs.runCommandLocal "env-paths" {} (let + profilePath = makeBinPath (builtins.filter (x: x != "/nix/var/profiles/default") config.environment.profiles); + configEnvPath = + if builtins.hasAttr "PATH" config.env + then config.env.PATH + else null; + prevPath = + if (configEnvPath != null) + then config.env.PATH + ":" + profilePath + else profilePath; + baseOut = '' + echo "PATH=$PATH;" > $out + ''; + printOuts = + if config.modules.macos.brew.enable + then '' + if [[ -x ${config.homebrew.brewPrefix}/brew ]]; then + ${config.homebrew.brewPrefix}/brew shellenv bash > $out + else + ${baseOut} + fi + '' + else baseOut; + in '' + PATH="" + if [ -x /usr/libexec/path_helper ]; then + eval $(/usr/libexec/path_helper -s) + else + PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin + fi + ${printOuts} + echo 'PATH=${prevPath}''${PATH:+:}$PATH; export PATH' >> $out + ''); +in { + config = { + environment = mkMerge [ + { + profiles = mkOrder 800 ["${config.home.stateDir}/nix/profile"]; + extraInit = mkOrder 100 ". ${env-paths}\n"; + } + (mkIf config.modules.shell.fish.enable { + etc."fish/nixos-env-preinit.fish".text = mkMerge [ + (lib.mkBefore '' + set -g __nixos_path_original $PATH + '') + (lib.mkAfter '' + function __nixos_path_fix -d "fix PATH value" + set -l result (string replace '$HOME' "$HOME" $__nixos_path_original) + for elt in $PATH + if not contains -- $elt $result + set -a result $elt + end + end + set -g PATH $result + end + '') + ]; + }) + ]; + programs = { + bash.interactiveShellInit = '' + # /etc/profile 的执行导致 bash 中 PATH 出现问题,这里重新声明 PATH + . ${env-paths} + ''; + fish.shellInit = '' + __nixos_path_fix + ''; + }; + }; +} diff --git a/modules/optionals/os.nix b/modules/optionals/os.nix index e6ddab1..5ca90ea 100644 --- a/modules/optionals/os.nix +++ b/modules/optionals/os.nix @@ -88,35 +88,6 @@ in { users.users.${config.user.name} = mkAliasDefinitions options.user; - environment.extraInit = mkOrder 10 (let - inherit (pkgs.stdenvNoCC) isAarch64 isAarch32 isDarwin; - darwinPath = optionalString isDarwin (let - brewHome = - if isAarch64 || isAarch32 - then "/opt/homebrew/bin" - else "/usr/local/bin"; - prevPath = - builtins.replaceStrings ["$USER" "$HOME"] [config.user.name homedir] - (pkgs.lib.makeBinPath (builtins.filter (x: x != "/nix/var/nix/profiles/default") config.environment.profiles)); - in '' - PATH="" - eval $(/usr/libexec/path_helper -s) - [ -d ${brewHome} ] && eval $(${brewHome}/brew shellenv) - PATH=${prevPath}''${PATH:+:}$PATH - ''); - in - '' - ${darwinPath} - '' - + concatStringsSep "\n" (mapAttrsToList (n: v: ( - if "${n}" == "PATH" - then ''export ${n}="${v}:''${PATH:+:}$PATH"'' - else ''export ${n}="${v}"'' - )) - config.env) - + optionalString (config.nix.envVars != {}) '' - unset all_proxy http_proxy https_proxy - ''); # 用来提示还有那些可以规范的文件。如何使用, 使用 my-xdg 脚本取代 # environment.systemPackages = [pkgs.xdg-ninja]; modules.xdg.value = { @@ -134,6 +105,20 @@ in { then "/tmp/user/${toString config.user.uid}" else "/run/user/${toString config.user.uid}"; }; + environment = { + extraInit = mkOrder 300 '' + ${concatStringsSep "\n" (mapAttrsToList (n: v: ( + if "${n}" == "PATH" + then optionalString pkgs.stdenvNoCC.isLinux ''export ${n}="${v}''${PATH:+:}$PATH"'' + else ''export ${n}="${v}"'' + )) + config.env)} + ${optionalString (config.nix.envVars != {}) '' + unset all_proxy http_proxy https_proxy + ''} + ''; + variables = config.modules.xdg.value; + }; } (mkIf config.modules.gui.enable { fonts.packages = config.modules.gui.fonts;