“安装包”是指评估 Nix 构建表达式(使用 nix-env
、nix-shell -p
等。 ) 从源代码构建而不是使用 substitute .
还有 cross-posted to Unix& Linux因为,作为Charles Duffy pointed out ,如果它是关于命令行工具或配置的,它更符合主题。仍然把它留在这里,因为我假设强制一个包总是从源代码编译是可能的,只使用 Nix 语言,我只是还不知道如何。 (或者如果实际上不可能,有人会指出来,那么这个问题就属于这里。)
最佳答案
要么设置substitute
option在 nix.conf
中为 false
(默认为 true
)或在调用 Nix 命令时使用 --option substitute false
。
nix-env --options substitute false -i hello
nix-shell --options substitute false -p hello
可能不是您要找的机器人
正如 Robert Hensing(comment、chat)、Henri Menke(comment)和 Vladimír Çunát(comment)所指出的,这可能不是您真正想要的。
详细说明:我一直自信地使用最基本的 Nix 功能,但到了需要维护和部署用 C 编写的大型应用程序的自定义分支的地步,这在一开始就非常令人生畏。
试图以最简单的方式解决问题 fetch我的 fork 并用新的源代码重新构建它,所以我把它归结为这个问题。虽然,我怀疑对我来说正确的方向是 Nixpkgs/Create and debug packages在NixOS Wiki .
只重新构建包本身
Vladimír Čunát commented “禁用替代品会让你重建本地丢失的所有东西,尽管我怀疑提出这样问题的人通常只想重建指定的包本身。”
(这可能是通过 nix-build
或“只是”覆盖原始包来实现的,但可能是错误的。后者在 NixOS 维基文章 Development environment with nix-shell
中被提及(甚至可能被证明?),但未能仔细阅读它。)
再现性测试
如果他们想确保后续构建是确定性的,他们可能会提出同样的问题。作为Henri Menke comments ,应该为此使用 nix-build --check
。
--check
选项很容易被忽略; man nix-build
中没有记录或在 nix-build
在Nix manual ,但在 nix-store --realize
因为(如 man nix-build
解释的那样):
nix-build
is essentially a wrapper aroundnix-instantiate
(to translate a high-level Nix expression to a low-level store derivation) andnix-store --realise
(to build the store derivation) [and so] all options not listed here are passed tonix-store --realise
, except for--arg
and--attr
/-A
which are passed tonix-instantiate
.
请参阅 Nix manual 中的详细示例在 18.1. Spot-Checking Build Determinism和 the next section right after it .
the nix.conf
section下substitute
配置选项的相关部分来自 Nix manual :
Name
nix.conf
— Nix configuration fileDescription
Nix reads settings from two configuration files:
The system-wide configuration file
sysconfdir/nix/nix.conf
(i.e./etc/nix/nix.conf
on most systems), or$NIX_CONF_DIR/nix.conf
ifNIX_CONF_DIR
is set.The user configuration file
$XDG_CONFIG_HOME/nix/nix.conf
, or~/.config/nix/nix.conf
ifXDG_CONFIG_HOME
is not set.You can override settings on the command line using the
--option
flag, e.g.--option keep-outputs false
.The following settings are currently available:
[..]
substitute
If set to true (default), Nix will use binary substitutes if available. This option can be disabled to force building from source.
(以前称为 use-binary-caches
。)
<支持> 注意事项
将 substitute
设置为 false
(使用 --options
或在 nix.conf
中)不会如果命令多次发出,请重新编译包。也就是说,上面的hello
会第一次从源代码编译,然后如果再次发出命令,它将访问已经存在的存储路径。
这是它变得模糊的地方:很明显没有重新编译发生,因为除非包的 Nix 构建表达式不改变,存储输出哈希也不会改变,使下一个编译输出等同于前一个,因此这个 Action 是多余的。
因此,如果有人想对软件包进行一些简单的修改,并且只是想在本地尝试一下(例如,使用 nix-shell
),那么就必须使用 -I nixpkgs =a/local/nixpkgs/dir
获取这些更改 - 并最终进行重新编译?还是应该使用 nix-build
?
关于nix - 如何通过在本地构建它们而不是下载预构建的二进制文件来强制 Nix 为 "install packages"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66175268/