Skip to content

Commit

Permalink
fix: PATH is broken for fish shell
Browse files Browse the repository at this point in the history
  • Loading branch information
shanyouli committed Oct 20, 2024
1 parent 9b2713a commit f9e4e28
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 33 deletions.
4 changes: 0 additions & 4 deletions modules/darwin/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
79 changes: 79 additions & 0 deletions modules/darwin/shell.nix
Original file line number Diff line number Diff line change
@@ -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
'';
};
};
}
43 changes: 14 additions & 29 deletions modules/optionals/os.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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;
Expand Down

0 comments on commit f9e4e28

Please sign in to comment.