使用nix-shell --command执行命令遭权限拒绝,手动执行正常,原因何在?
问题原因与解决方案
问题出在命令替换的执行时机:你原来的命令中,$(find . -name '*.hs')是在调用nix-shell的外部shell里执行的,虽然文件名会传递给nix-shell内的ormolu,但特殊场景下(比如文件名含空格、或nix-shell环境的隐式限制)会触发权限错误。而手动进入shell后执行时,find和ormolu都在同一个nix-shell环境中运行,因此无问题。
可行解决方案
- 方案1:用单引号包裹
--command的全部内容,让find在nix-shell内部执行:
nix-shell -p ormolu --command 'ormolu --mode check $(find . -name "*.hs")'
- 方案2:用
xargs传递文件名,既避免命令替换时机问题,又兼容带空格的文件名:
find . -name '*.hs' | xargs nix-shell -p ormolu --command 'ormolu --mode check "$@"' --
- 方案3:显式通过bash执行命令,确保内部完整解析:
nix-shell -p ormolu --command "bash -c 'ormolu --mode check \$(find . -name \"*.hs\")'"
内容的提问来源于stack exchange,提问作者Yarick
相关产品推荐
相关产品推荐

