FAQ/zh: Difference between revisions

Weijia (talk | contribs)
Created page with "<syntaxhighlight lang="bash"> nix-env --list-generations -p /nix/var/nix/profiles/per-user/root/channels 18 2014-04-17 09:16:28 19 2014-06-13 10:31:24 20 2014-08-12 19:09:20 (current) </syntaxhighlight> 回滚到上一代的方法如下:"
Ardenet (talk | contribs)
Created page with "要使用 -Og 和 -g 参数构建软件包,且不去除调试符号,请使用:"
 
(37 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
{{cleanup}}
为了避免重复回答那些经常被问到的问题以及处理新手常遇到的麻烦,这些问题被收录在这里。
为了避免重复回答那些经常被问到的问题以及处理新手常遇到的麻烦,这些问题被收录在这里。


<div lang="en" dir="ltr" class="mw-content-ltr">
http://unix.stackexchange.com/questions/tagged/nixos 也可用于提问。
http://unix.stackexchange.com/questions/tagged/nixos can also be used for questions.
</div>


<span id="Why_is_there_a_new_wiki?_What_is_with_nixos.wiki?"></span>
<span id="Why_is_there_a_new_wiki?_What_is_with_nixos.wiki?"></span>
== 为什么创建了一个新的 wiki?nixos.wiki 怎么了? ==
=== 为什么创建了一个新的维基?nixos.wiki 怎么了? ===


旧的 wiki(nixos.wiki)存在以下几个问题:
旧的 wiki(nixos.wiki)存在以下几个问题:
Line 15: Line 14:
** PHP 7.3.33(生命周期结束于 2021 年 12 月)
** PHP 7.3.33(生命周期结束于 2021 年 12 月)
** ICU 64.2
** ICU 64.2
* Cloudflare 的 DDoS 防御有时会导致 wiki 编辑失败。
* Cloudflare 的 DDoS 防御有时会导致维基编辑失败。
* 没有所见即所得(WYSIWYG)的编辑器。
* 没有所见即所得(WYSIWYG)的编辑器。
* wiki 的基础设施本应在上线后公开,但最终未能公开。
* 维基的基础设施本应在上线后公开,但最终未能公开。


我们曾通过多种渠道(电子邮件、Matrix)多次尝试解决这些问题,历时多年,但始终未能得到直接答复。最后一次联系是由 zimbatm 代表 NixOS 基金会与维护者沟通,询问关于新 wiki 合作的可能性。答案是否定的。随着旧 wiki 不断恶化且维护者未作回应,将内容分叉到一个新 wiki 成为了唯一的解决方法。
我们曾通过多种渠道(电子邮件、Matrix)多次尝试解决这些问题,历时多年,但始终未能得到直接答复。最后一次联系是由 zimbatm 代表 [[NixOS Foundation|NixOS 基金会]] 与维护者沟通,询问关于新维基合作的可能性。答案是否定的。随着旧维基不断恶化且维护者未作回应,将内容分叉到一个新维基成为了唯一的解决方法。


另请参阅:
另请参阅:
Line 26: Line 25:


<span id="Why_is_Nix_written_in_C++_rather_than_a_functional_language_like_Haskell?"></span>
<span id="Why_is_Nix_written_in_C++_rather_than_a_functional_language_like_Haskell?"></span>
== 为什么 Nix 是用 C++ 编写的,而不是像 Haskell 这样的函数式语言? ==
=== 为什么 Nix 是用 C++ 编写的,而不是使用像 Haskell 这样的函数式语言? ===


主要是因为 Nix 旨在轻量级、易于学习且具有良好的可移植性(零依赖)。
主要是因为 Nix 旨在轻量级、易于学习且具有良好的可移植性(零依赖)。


<span id="How_to_keep_build-time_dependencies_around_/_be_able_to_rebuild_while_being_offline?"></span>
<span id="How_to_keep_build-time_dependencies_around_/_be_able_to_rebuild_while_being_offline?"></span>
== 如何保留构建时依赖 / 在离线状态下重新构建? ==
=== 如何保留构建时依赖/在离线状态下重新构建? ===


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix"># /etc/nixos/configuration.nix
<syntaxhighlight lang="nix"># /etc/nixos/configuration.nix
{ config, pkgs, lib, ... }:
{ config, pkgs, lib, ... }:
Line 44: Line 42:
   };
   };
}</syntaxhighlight>
}</syntaxhighlight>
Check 'man configuration.nix' for these options. Rebuild for these options to take effect:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
请查阅“man configuration.nix”文件以了解这些选项。重新构建系统以使这些选项生效:
 
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
List all store paths that form the system closure and realise them:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
列出构成系统闭包的所有存储路径并实例化它们:
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nix-store -qR $(nix-instantiate '<nixpkgs/nixos>' -A system) | xargs nix-store -r
nix-store -qR $(nix-instantiate '<nixpkgs/nixos>' -A system) | xargs nix-store -r
warning: you did not specify `--add-root'; the result might be removed by the garbage collector
warning: you did not specify `--add-root'; the result might be removed by the garbage collector
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<build output and list of successfully realised paths>
<build output and list of successfully realised paths>
</syntaxhighlight>
</syntaxhighlight>
Repeat for your user and further profiles:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
对您的用户和其他配置文件重复该步骤:
 
<syntaxhighlight lang="bash">nix-store -qR ~/.nix-profile | xargs nix-store -r</syntaxhighlight>
<syntaxhighlight lang="bash">nix-store -qR ~/.nix-profile | xargs nix-store -r</syntaxhighlight>
The warning can be ignored for profiles that are listed/linked in ''/nix/var/nix/profiles/'' or one of its subdirectories.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
对于列在 ''/nix/var/nix/profiles/'' 或其子目录中的配置文件,可以忽略此警告。
Consult man pages of nix-store and nix-instantiate for further information.
 
</div>
有关更多信息,请参阅 nix-store nix-instantiate 的手册。


<span id="Why_-_instead_of_-?"></span>
<span id="Why_-_instead_of_-?"></span>
== 为什么使用 <hash>-<name> 而不是 <name>-<hash>? ==
=== 为什么使用 <hash>-<name> 而不是 <name>-<hash>? ===


在少数需要深入查看 /nix/store 的情况下,将哈希值的前几位放在开头更方便记忆和使用。
在少数需要深入查看 /nix/store 的情况下,将哈希值的前几位放在开头更方便记忆和使用。
Line 82: Line 73:
此外,由于开头部分长度一致,查看一系列软件包列表时在视觉上也更容易解析。
此外,由于开头部分长度一致,查看一系列软件包列表时在视觉上也更容易解析。


<div lang="en" dir="ltr" class="mw-content-ltr">
如果你仍然想知道原因,请在 shell 中运行 <code>ls -1 /nix/store | sort -R -t - -k 2 | less</code> 命令。''(? 不清楚)''
If you still wonder why, run <code>ls -1 /nix/store | sort -R -t - -k 2 | less</code> in your shell. ''(? unclear)''
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
如果你不经常进行垃圾回收,或者正在测试编译变体,可能会发生这种情况:
This is what might happen if you don't garbage collect frequently, or if you are testing compilation variants:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/
q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/
Line 102: Line 88:
xvs7y09jf7j48p6l0p87iypgpq470jqw-nixos-build-vms/
xvs7y09jf7j48p6l0p87iypgpq470jqw-nixos-build-vms/
</syntaxhighlight>
</syntaxhighlight>
</div>


<span id="I&#039;ve_updated_my_channel_and_something_is_broken,_how_can_I_rollback_to_an_earlier_channel?"></span>
<span id="I&#039;ve_updated_my_channel_and_something_is_broken,_how_can_I_rollback_to_an_earlier_channel?"></span>
== 我更新了我的 channel,但出现问题,如何回滚到之前的 channel? ==
=== 我更新了我的 channel出现问题,如何回滚到之前的 channel? ===


查看可用的各代 channel:
查看可用的各代 channel:
Line 115: Line 100:
20  2014-08-12 19:09:20  (current)
20  2014-08-12 19:09:20  (current)
</syntaxhighlight>
</syntaxhighlight>
回滚到上一代的方法如下:


<div lang="en" dir="ltr" class="mw-content-ltr">
回滚到上一代的方法:
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nix-env --rollback -p /nix/var/nix/profiles/per-user/root/channels
nix-env --rollback -p /nix/var/nix/profiles/per-user/root/channels
switching from generation 20 to 19
switching from generation 20 to 19
</syntaxhighlight>
</syntaxhighlight>
To switch to a particular generation:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
切换到指定世代:
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nix-env --switch-generation 18 -p /nix/var/nix/profiles/per-user/root/channels
nix-env --switch-generation 18 -p /nix/var/nix/profiles/per-user/root/channels
switching from generation 20 to 18
switching from generation 20 to 18
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="I&#039;m_working_on_a_new_package,_how_can_I_build_it_without_adding_it_to_nixpkgs?"></span>
== I'm working on a new package, how can I build it without adding it to nixpkgs? ==
=== 我正在开发一个新软件包,如何在不将其添加到 nixpkgs 的情况下构建它? ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; callPackage ./mypackage.nix { }'</syntaxhighlight>
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; callPackage ./mypackage.nix { }'</syntaxhighlight>
You can replace callPackage with callPackage_i686 to build the 32-bit version of your package on a 64-bit system if you want to test that.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
如果您想在 64 位系统上测试,可以将 callPackage 替换为 callPackage_i686 来构建 32 位版本的软件包。
== How can I compile a package with debugging symbols included? ==
 
</div>
<span id="How_can_I_compile_a_package_with_debugging_symbols_included?"></span>
=== 我如何编译包含调试符号的包?===


<div lang="en" dir="ltr" class="mw-content-ltr">
要使用 -Og -g 参数构建软件包,且不去除调试符号,请使用:
To build a package with -Og and -g, and without stripping debug symbols use:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage'</syntaxhighlight>
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage'</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
另见 [[Debug Symbols]]
See also [[Debug Symbols]]
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="How_can_I_force_a_rebuild_from_source_even_without_modifying_the_nix_expression?"></span>
== How can I force a rebuild from source even without modifying the nix expression? ==
=== 即使不修改 nix 表达式,我如何强制从源代码重建?===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
root 用户身份运行 nix-build 时,可以使用 --check 标志:
As root you can run nix-build with the --check flag:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">sudo nix-build --check -A ncdu</syntaxhighlight>
<syntaxhighlight lang="bash">sudo nix-build --check -A ncdu</syntaxhighlight>
== How can I manage software with nix-env like with configuration.nix? ==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="How_can_I_manage_software_with_nix-env_like_with_configuration.nix?"></span>
There are many ways, one is the following:
=== 我如何使用 nix-env 管理软件,就像使用 configuration.nix 一样?===
</div>
 
有很多方法,其中一种如下:


<div lang="en" dir="ltr" class="mw-content-ltr">
<ol style="list-style-type: decimal;">
<ol style="list-style-type: decimal;">
<li><p>Create a meta package called ''userPackages'' your ''~/.config/nixpkgs/config.nix'' file with the packages you would like to have in your environment:</p>
<li><p><span lang="en" dir="ltr" class="mw-content-ltr">Create a meta package called ''userPackages'' your ''~/.config/nixpkgs/config.nix'' file with the packages you would like to have in your environment:</span></p>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
with (import <nixpkgs> {});
with (import <nixpkgs> {});
Line 191: Line 159:
}
}
</syntaxhighlight></li>
</syntaxhighlight></li>
<li><p>Install all specified packages using this command:</p>
<li><p><span lang="en" dir="ltr" class="mw-content-ltr">Install all specified packages using this command:</span></p>
<syntaxhighlight lang="bash">nix-env -iA userPackages -f '<nixpkgs>'</syntaxhighlight></li></ol>
<syntaxhighlight lang="bash">nix-env -iA userPackages -f '<nixpkgs>'</syntaxhighlight></li></ol>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 202: Line 169:
Another way is using [[Home Manager]].
Another way is using [[Home Manager]].
</div>
</div>
<span id="I&#039;ve_downloaded_a_binary,_but_I_can&#039;t_run_it,_what_can_I_do?"></span>
=== 我下载了一个二进制文件,但是无法运行它,我该怎么办?===


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== I've downloaded a binary, but I can't run it, what can I do? ==
Binaries normally do not work out of the box when you download them because they normally just assume that libraries can be found in hardcoded paths such as <code>/lib</code>. However this assumption is incorrect on NixOS systems due to the inner workings of <code>nix</code> - there is no default path, everything gets set to the corresponding version on compile time.
Binaries normally do not work out of the box when you download them because they normally just assume that libraries can be found in hardcoded paths such as <code>/lib</code>. However this assumption is incorrect on NixOS systems due to the inner workings of <code>nix</code> - there is no default path, everything gets set to the corresponding version on compile time.
</div>
</div>
Line 220: Line 189:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
programs.nix-ld = {
programs.nix-ld = {
Line 226: Line 194:
   libraries = [ pkgs.zlib pkgs.openssl ];
   libraries = [ pkgs.zlib pkgs.openssl ];
};</syntaxhighlight>
};</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 236: Line 203:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
programs.nix-ld = {
programs.nix-ld = {
   enable = true;
   enable = true;
   libraries = pkgs.steam-run.fhsenv.args.multiPkgs pkgs;
   libraries = pkgs.steam-run.args.multiPkgs pkgs;
};</syntaxhighlight>
};</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
This uses the libraries that are used by [[Steam]] to simulate a traditional Linux FHS environment to run games in. It's a [https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/games/steam/fhsenv.nix big list] that usually contains all the libraries your binary needs to run.
This uses the libraries that are used by [[Steam]] to simulate a traditional Linux FHS environment to run games in. It's a [https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/by-name/st/steam/package.nix big list] that usually contains all the libraries your binary needs to run.
</div>
</div>


Line 252: Line 217:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# mybinaryprogram.nix
# mybinaryprogram.nix
Line 273: Line 237:
   '';
   '';
}</syntaxhighlight>
}</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
This can be built with:
This can be built with:
</div>
</div>
<syntaxhighlight lang="bash">nix-build mybinaryprogram.nix</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nix-build mybinaryprogram.nix</syntaxhighlight>
And run with:
And run with:
</div>
</div>
<syntaxhighlight lang="bash">./result/bin/mybinaryprogram</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">./result/bin/mybinaryprogram</syntaxhighlight>
Another possibility is using a FHS-compatible Sandbox with [https://nixos.org/nixpkgs/manual/#sec-fhs-environments buildFHSUserEnv]
Another possibility is using a FHS-compatible Sandbox with [https://nixos.org/nixpkgs/manual/#sec-fhs-environments buildFHSUserEnv]
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# fhsUser.nix
# fhsUser.nix
Line 308: Line 275:
   runScript = "bash";
   runScript = "bash";
}).env</syntaxhighlight>
}).env</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
the sandbox can be entered with
the sandbox can be entered with
</div>
</div>
<syntaxhighlight lang="bash">nix-shell fhsUser.nix</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nix-shell fhsUser.nix</syntaxhighlight>
<br>
If your target application can't find shared libraries inside buildFHSUserEnv, you may run [https://github.com/lexleogryfon/de-generate nix-de-generate] for target application inside FHS, which will generate newenv.nix file, an nix-expression of buildFHSUserEnv with resolved dependencies for shared libraries.
If your target application can't find shared libraries inside buildFHSUserEnv, you may run [https://github.com/lexleogryfon/de-generate nix-de-generate] for target application inside FHS, which will generate newenv.nix file, an nix-expression of buildFHSUserEnv with resolved dependencies for shared libraries.
== What are channels and how do they get updated? ==
</div>
 
<span id="What_are_channels_and_how_do_they_get_updated?"></span>
=== 什么是频道以及如何更新?===
 
{{main|Channel branches}}
{{main|Channel branches}}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 359: Line 331:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
 
<ol start="2" style="list-style-type: decimal;">
<ol start="2" style="list-style-type: decimal;">
<li>Once the job succeeds at a particular nixpkgs commit, '''cache.nixos.org''' will download binaries from '''hydra.nixos.org'''.</li>
<li><span lang="en" dir="ltr" class="mw-content-ltr">Once the job succeeds at a particular nixpkgs commit, '''cache.nixos.org''' will download binaries from '''hydra.nixos.org'''.</span></li>
<li>Once the above download completes, the channel updates.</li></ol>
<li><span lang="en" dir="ltr" class="mw-content-ltr">Once the above download completes, the channel updates.</span></li>
</div>
</ol>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 369: Line 341:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="How_do_I_know_where&#039;s_nixpkgs_channel_located_and_at_which_commit?"></span>
== How do I know where's nixpkgs channel located and at which commit? ==
=== 我如何知道 nixpkgs 频道位于哪里以及在哪个提交?===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 380: Line 351:
If you want to know where <nixpkgs> is located:
If you want to know where <nixpkgs> is located:
</div>
</div>
<syntaxhighlight lang="bash">nix-instantiate --find-file nixpkgs</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nix-instantiate --find-file nixpkgs</syntaxhighlight>
To know the commit, open the .version-suffix file in the nixpkgs location. The hash after the dot is the git commit.
To know the commit, open the .version-suffix file in the nixpkgs location. The hash after the dot is the git commit.
</div>
</div>
<span id="Nixpkgs_branches"></span>
=== Nixpkgs 分支 ===


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== Nixpkgs branches ==
Branches on the nixpkgs repo have a relationship with channels, but that relationship is not 1:1.
Branches on the nixpkgs repo have a relationship with channels, but that relationship is not 1:1.
</div>
</div>
Line 402: Line 376:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
So in short, the <code>relase-XX.YY</code> branches have not been run through Hydra yet, whereas the <code>nixos-XX.YY</code> ones have.
So in short, the <code>relase-XX.YY</code> branches have not been run through Hydra yet, whereas the <code>nixos-XX.YY</code> ones have.
</div>
<span id="There&#039;s_an_updated_version_for_$software_on_nixpkgs_but_not_in_channels,_how_can_I_use_it?"></span>
=== nixpkgs 上有 $software 的更新版本,但在频道中没有,我该如何使用它?===
<div lang="en" dir="ltr" class="mw-content-ltr">
You can jump the queue and use <code>nix-shell</code> with a <code>NIX_PATH</code> pointing to a tarball of the channel to get a shell for that software. Some building may occur. This will not work for system services.
</div>
<syntaxhighlight lang="command">
NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/release-17.09.tar.gz nix-shell -p $software
</syntaxhighlight>
<span id="There&#039;s_an_updated_version_for_$software_on_the_unstable_branch,_but_I_use_stable,_how_can_I_use_it?"></span>
=== 不稳定分支上有软件 $software 的更新版本,但我用的是稳定版,我该如何使用它? ===
<div lang="en" dir="ltr" class="mw-content-ltr">
Before going ahead with this, note that firstly, this likely means that the package you intend to update has had a major version change. If you have used it previously, there is a chance that your existing data either will not work with the new version or will need to be migrated; If in doubt, consult the upstream documentation of the package.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
Secondly, while you're less likely to run into issues on NixOS than on, for example, Debian when installing packages from different releases, it's not impossible.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
Nix ensures that libraries and (usually) runtime dependencies of packages are kept separate, so that you can trivially have many versions of those dependencies installed, without affecting the versions of said dependencies used by important system components. This ensures that you cannot accidentally break your package manager by, say, updating Python, as is quite common on other distros.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
Nix cannot however ensure that there will be no incompatibilities with services of which there can inherently be only one running instance. As an example, if you try to use a package from unstable on a stable system that requires a feature in systemd that is not yet present in the systemd version on stable, this package will not work; it's simply not possible to run two different versions of systemd simultaneously.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== There's an updated version for $software on nixpkgs but not in channels, how can I use it? ==
Nonetheless, it's quite uncommon that end-user facing applications rely on such singleton services, or at the very least they will typically have internal backwards compatibility. As such, mixing channels is usually unproblematic in practice, and even if not, NixOS' rollback features make it trivial to recover from problems should they occur.
</div>
</div>
<span id="Using_channels"></span>
==== 使用频道 ====


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
You can jump the queue and use <code>nix-shell</code> with a <code>NIX_PATH</code> pointing to a tarball of the channel to get a shell for that software. Some building may occur. This will not work for system services.
First we need to add the unstable channel to our system channels:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="command">
{{Warning|`nixos-rebuild --upgrade` will by default only update the channel named `nixos`, which this new channel is not. Use `nixos-rebuild --upgrade-all` instead.}}
NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/release-17.09.tar.gz nix-shell -p $software
</div>
 
<syntaxhighlight lang="console">
$ sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable
$ sudo nix-channel --update
</syntaxhighlight>
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
Then we can import this channel using the angle-bracket notation to refer to it:
</div>
</div>
<syntaxhighlight lang="nixos"># configuration.nix
{
  config,
  pkgsUnstable,
  ...
}: {
  # We add a new `pkgsUnstable` to the module arguments; this allows
  # us to easily use `pkgsUnstable` in other modules as well, without
  # having to evaluate it again.
  _module.args.pkgsUnstable = import <nixos-unstable> { inherit (config.nixpkgs) config; };
  environment.systemPackages = [
    # Once we have created our `pkgsUnstable`, we can easily use
    # packages from it wherever NixOS modules expect derivations
    pkgsUnstable.hello
  ];
}</syntaxhighlight>
<span id="Using_flakes"></span>
==== 使用 Flakes ====
<div lang="en" dir="ltr" class="mw-content-ltr">
We simply add the unstable branch to our flake inputs, and pass them into the NixOS module system using <code>specialArgs</code>:
</div>
<syntaxhighlight lang="nix">
# flake.nix
{
  inputs = {
    nixpkgs.url = "https://channels.nixos.org/nixos-25.05/nixexprs.tar.xz";
    nixpkgs-unstable.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz";
  };
  outputs = { nixpkgs, ... } @ inputs: {
    # Note that the hostname "nixos" and the system tuple used here are
    # examples.
    nixosConfigurations."nixos" = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        ./configuration.nix
      ];
      # Any attributes of `specialArgs` will be added to our NixOS module
      # arguments.
      #
      # We've bound `nixpkgs-unstable` to the `inputs` variable using the `@`
      # syntax; if we add any other flake inputs in the future those will also
      # be added to our module arguments.
      specialArgs.flake-inputs = inputs;
    };
  };
}
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== How do I install a specific version of a package for build reproducibility etc.? ==
Using this in <code>configuration.nix</code> then looks as follows:
</div>
</div>
<syntaxhighlight lang="nixos">
# configuration.nix
{
  pkgs,
  flake-inputs,
  ...
}: {
  environment.systemPackages = [
    flake-inputs.nixpkgs-unstable.legacyPackages.${pkgs.system}.hello
  ];
}
</syntaxhighlight>
<span id="How_do_I_install_a_specific_version_of_a_package_for_build_reproducibility_etc.?"></span>
=== 如何安装特定版本的软件包以实现构建可重复性等?===


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 430: Line 515:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="An_error_occurs_while_fetching_sources_from_an_url,_how_do_I_fix_it?"></span>
== An error occurs while fetching sources from an url, how do I fix it? ==
=== 从 URL 获取源时发生错误,我该如何修复?===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 439: Line 523:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
If that fails you can update the url in the nix expression yourself. [https://nixos.org/wiki/FAQ#How_do_I_know_where.27s_nixpkgs_channel_located_and_at_which_commit.3F Navigate to your channel's expressions] and find the package in one of the subdirectories. Edit the respective ''default.nix'' file by altering the ''url'' and ''sha256''. You can use <code>nix-prefetch-url url</code> to get the SHA-256 hash of source distributions.
If that fails you can update the url in the nix expression yourself. [[#How_do_I_know_where's_nixpkgs_channel_located_and_at_which_commit?|Navigate to your channel's expressions]] and find the package in one of the subdirectories. Edit the respective ''default.nix'' file by altering the ''url'' and ''sha256''. You can use <code>nix-prefetch-url url</code> to get the SHA-256 hash of source distributions.
</div>
</div>


Line 449: Line 533:
start a new shell with a private mount namespace (Linux-only)
start a new shell with a private mount namespace (Linux-only)
</div>
</div>
<syntaxhighlight lang="bash">sudo unshare -m bash</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">sudo unshare -m bash</syntaxhighlight>
remount the filesystem with write privileges (as root)
remount the filesystem with write privileges (as root)
</div>
</div>
<syntaxhighlight lang="bash">mount -o remount,rw /nix/store</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">mount -o remount,rw /nix/store</syntaxhighlight>
update the file
update the file
</div>
</div>
<syntaxhighlight lang="bash">nano <PATH_TO_PACKAGE>/default.nix</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nano <PATH_TO_PACKAGE>/default.nix</syntaxhighlight>
exit to shell where /nix/store is still mounted read-only
exit to shell where /nix/store is still mounted read-only
</div>
</div>
<syntaxhighlight lang="bash">exit</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">exit</syntaxhighlight>
Be sure to [https://github.com/NixOS/nixpkgs/issues report the incorrect url] or [https://github.com/NixOS/nixpkgs/pulls fix it yourself].
Be sure to [https://github.com/NixOS/nixpkgs/issues report the incorrect url] or [https://github.com/NixOS/nixpkgs/pulls fix it yourself].
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="How_do_I_know_the_sha256_to_use_with_fetchgit,_fetchsvn,_fetchbzr_or_fetchcvs?"></span>
== How do I know the sha256 to use with fetchgit, fetchsvn, fetchbzr or fetchcvs? ==
=== 我如何知道要与 fetchgit、fetchsvn、fetchbzr 或 fetchcvs 一起使用的 sha256?===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 482: Line 569:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nix-prefetch-git https://git.zx2c4.com/password-store</syntaxhighlight>
<syntaxhighlight lang="bash">nix-prefetch-git https://git.zx2c4.com/password-store</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 490: Line 575:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Should_I_use_http://hydra.nixos.org/_as_a_binary_cache?"></span>
== Should I use http://hydra.nixos.org/ as a binary cache? ==
=== 我应该使用 http://hydra.nixos.org/ 作为二进制缓存吗?===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
不可以。截至 2017 年,所有构建工件都直接推送到 http://cache.nixos.org/ 并在那里可用,因此将 http://hydra.nixos.org/ 设置为二进制缓存不再具有任何功能。
No. As of 2017, all build artifacts are directly pushed to http://cache.nixos.org/ and are available there, therefore setting http://hydra.nixos.org/ as a binary cache no longer serves any function.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="I&#039;m_trying_to_install_NixOS_but_my_WiFi_isn&#039;t_working_and_I_don&#039;t_have_an_ethernet_port"></span>
== I'm trying to install NixOS but my WiFi isn't working and I don't have an ethernet port ==
=== 我正在尝试安装 NixOS,但我的 WiFi 无法使用,而且我没有以太网端口 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 514: Line 595:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="How_can_I_disable_the_binary_cache_and_build_everything_locally?"></span>
== How can I disable the binary cache and build everything locally? ==
=== 我如何禁用二进制缓存并在本地构建所有内容?===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 526: Line 606:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nixos-rebuild switch --option binary-caches ''</syntaxhighlight>
<syntaxhighlight lang="bash">nixos-rebuild switch --option binary-caches ''</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="How_do_I_enable_sandboxed_builds_on_non-NixOS?"></span>
== How do I enable sandboxed builds on non-NixOS? ==
=== 如何在非 NixOS 上启用沙盒构建?===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 538: Line 615:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# /etc/nix/nix.conf
# /etc/nix/nix.conf
Line 544: Line 620:
build-sandbox-paths = $(nix-store -qR $(nix-build '<nixpkgs>' -A bash) | xargs echo /bin/sh=$(nix-build '<nixpkgs>' -A bash)/bin/bash)
build-sandbox-paths = $(nix-store -qR $(nix-build '<nixpkgs>' -A bash) | xargs echo /bin/sh=$(nix-build '<nixpkgs>' -A bash)/bin/bash)
</syntaxhighlight>
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
On NixOS set the following in ''configuration.nix'':
On NixOS set the following in ''configuration.nix'':
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
nix.settings.sandbox = true;
nix.settings.sandbox = true;
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 557: Line 633:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="How_can_I_install_a_package_from_unstable_while_remaining_on_the_stable_channel?"></span>
== How can I install a package from unstable while remaining on the stable channel? ==
=== 我如何在稳定频道上安装来自不稳定频道的软件包?===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 565: Line 640:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nix-shell -I nixpkgs=channel:nixpkgs-unstable -p somepackage</syntaxhighlight>
<syntaxhighlight lang="bash">nix-shell -I nixpkgs=channel:nixpkgs-unstable -p somepackage</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
It is possible to have multiple nix-channels simultaneously. To add the unstable channel with the specifier ''unstable'',
It is possible to have multiple nix-channels simultaneously. To add the unstable channel with the specifier ''unstable'',
</div>
</div>
<syntaxhighlight lang="bash">sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable</syntaxhighlight>
After updating the channel
After updating the channel
</div>
</div>
<syntaxhighlight lang="bash">sudo nix-channel --update nixos-unstable</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">sudo nix-channel --update nixos-unstable</syntaxhighlight>
queries via <code>nix-env</code> will show packages from both ''stable'' and ''unstable''. Use this to install unstable packages into your user environment. The following snippet shows how this can be done in ''configuration.nix''.
queries via <code>nix-env</code> will show packages from both ''stable'' and ''unstable''. Use this to install unstable packages into your user environment. The following snippet shows how this can be done in ''configuration.nix''.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ config, pkgs, ... }:
{ config, pkgs, ... }:
Line 592: Line 666:
}
}
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
This only changes what version of <code>PACKAGE_NAME</code> is available on <code>$PATH</code>. If the package you want to take from unstable is installed through a NixOS module, you must use [[overlays]]:
This only changes what version of <code>PACKAGE_NAME</code> is available on <code>$PATH</code>. If the package you want to take from unstable is installed through a NixOS module, you must use [[overlays]]:
</div>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ config, pkgs, ... }:
{ config, pkgs, ... }:
Line 608: Line 683:
}
}
</syntaxhighlight>
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
Note that this will rebuild all packages depending on the overlaid package, which may be a lot. Some modules offer a <code>services.foo.package</code> to change the actual derivation used by the module without and overlay, and without recompiling dependencies ([https://nixos.org/manual/nixos/stable/options.html#opt-services.gvfs.package example]).
Note that this will rebuild all packages depending on the overlaid package, which may be a lot. Some modules offer a <code>services.foo.package</code> to change the actual derivation used by the module without and overlay, and without recompiling dependencies ([https://nixos.org/manual/nixos/stable/options.html#opt-services.gvfs.package example]).
</div>
</div>
Line 613: Line 690:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
If you want to install unfree packages from unstable you need to also set allowUnfree by replacing the import statment above with:
If you want to install unfree packages from unstable you need to also set allowUnfree by replacing the import statment above with:
</div>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
import <nixos-unstable> { config = { allowUnfree = true; }; }
import <nixos-unstable> { config = { allowUnfree = true; }; }
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="I&#039;m_unable_to_connect_my_USB_HDD_|_External_HDD_is_failing_to_mount_automatically"></span>
== I'm unable to connect my USB HDD | External HDD is failing to mount automatically ==
=== 我无法连接我的 USB HDD | 外部 HDD 无法自动安装 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 630: Line 707:
</div>  
</div>  


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">su nano /etc/nixos/configuration.nix</syntaxhighlight>
<syntaxhighlight lang="bash">su nano /etc/nixos/configuration.nix</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Add this line to your configuration file.
Add this line to your configuration file.
</div>
<syntaxhighlight lang="bash">boot.extraModulePackages = [ config.boot.kernelPackages.exfat-nofuse ];</syntaxhighlight>
<syntaxhighlight lang="bash">boot.extraModulePackages = [ config.boot.kernelPackages.exfat-nofuse ];</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 643: Line 719:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 651: Line 725:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="What_is_the_origin_of_the_name_&quot;Nix&quot;"></span>
== What is the origin of the name "Nix" ==
=== “Nix” 这个名字的由来 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
The name <code>Nix</code> comes from the Dutch word [https://en.wiktionary.org/wiki/nix niks] which means ''nothing''. It reflects the fact that Nix derivations do not have access to anything that has not been explicitly declared as an input.<ref>Eelco Dolstra et al. “Nix: A Safe and Policy-Free System for Software Deployment.” LiSA (2004), https://pdfs.semanticscholar.org/5fd8/8f89bd8738816e62808a1b7fb12d3ab14a2f.pdf</ref>
The name <code>Nix</code> comes from the Dutch word [https://en.wiktionary.org/wiki/nix niks] which means ''nothing''. It reflects the fact that Nix derivations do not have access to anything that has not been explicitly declared as an input.<ref>Eelco Dolstra et al. “Nix: A Safe and Policy-Free System for Software Deployment.” LiSA (2004), https://pdfs.semanticscholar.org/5fd8/8f89bd8738816e62808a1b7fb12d3ab14a2f.pdf</ref>
</div>
</div>
<span id="What_does_it_mean_to_say_that_NixOS_is_&quot;immutable&quot;"></span>
=== NixOS 中的“不可变”是什么意思 ===


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== What does it mean to say that NixOS is "immutable" ==
Immutability is a property of data, in general, which means that the data cannot be modified after it is created. In the context of an operating system, it really means that certain parts of the system have this property. In the case of Nix and NixOS, that includes the Nix store, where files can be created but not modified after the time they are created. It does not apply to every part of the operating system, in that users can still modify their own files in their home directory, for example.
Immutability is a property of data, in general, which means that the data cannot be modified after it is created. In the context of an operating system, it really means that certain parts of the system have this property. In the case of Nix and NixOS, that includes the Nix store, where files can be created but not modified after the time they are created. It does not apply to every part of the operating system, in that users can still modify their own files in their home directory, for example.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="I&#039;m_getting_‘infinite_recursion’_errors_when_trying_to_do_something_clever_with_imports"></span>
== I'm getting ‘infinite recursion’ errors when trying to do something clever with <code>imports</code> ==
=== 当我尝试使用 <code>imports</code> 做一些巧妙的事情时,我得到了“无限递归”(infinite recursion)错误 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 684: Line 758:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{:FAQ/Libraries}}
{{:FAQ/Libraries}}
{{:FAQ/nix-env -iA}}
{{:FAQ/nix-env -iA}}
{{:FAQ/stateVersion}}
{{:FAQ/stateVersion}}
{{:FAQ/notfound}}
{{:FAQ/notfound}}
{{:FAQ/unfree}}
<!-- Transclude subpages -->
<!-- Transclude subpages -->
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="References"></span>
== References ==
== 参考 ==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[[Category:Cookbook]]
[[Category:Cookbook]]
</div>