FAQ/zh: Difference between revisions
No edit summary Tags: Mobile edit Mobile web edit |
Created page with "要使用 -Og 和 -g 参数构建软件包,且不去除调试符号,请使用:" |
||
| (30 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
<languages/> | <languages/> | ||
{{cleanup}} | |||
为了避免重复回答那些经常被问到的问题以及处理新手常遇到的麻烦,这些问题被收录在这里。 | 为了避免重复回答那些经常被问到的问题以及处理新手常遇到的麻烦,这些问题被收录在这里。 | ||
http://unix.stackexchange.com/questions/tagged/nixos 也可用于提问。 | |||
http://unix.stackexchange.com/questions/tagged/nixos | |||
<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> | ||
| Line 21: | Line 18: | ||
* 维基的基础设施本应在上线后公开,但最终未能公开。 | * 维基的基础设施本应在上线后公开,但最终未能公开。 | ||
我们曾通过多种渠道(电子邮件、Matrix)多次尝试解决这些问题,历时多年,但始终未能得到直接答复。最后一次联系是由 zimbatm 代表 [[NixOS Foundation|NixOS 基金会]] 与维护者沟通,询问关于新维基合作的可能性。答案是否定的。随着旧维基不断恶化且维护者未作回应,将内容分叉到一个新维基成为了唯一的解决方法。 | |||
我们曾通过多种渠道(电子邮件、Matrix)多次尝试解决这些问题,历时多年,但始终未能得到直接答复。最后一次联系是由 zimbatm 代表 NixOS 基金会与维护者沟通,询问关于新维基合作的可能性。答案是否定的。随着旧维基不断恶化且维护者未作回应,将内容分叉到一个新维基成为了唯一的解决方法。 | |||
另请参阅: | 另请参阅: | ||
| Line 37: | Line 32: | ||
=== 如何保留构建时依赖/在离线状态下重新构建? === | === 如何保留构建时依赖/在离线状态下重新构建? === | ||
<syntaxhighlight lang="nix"># /etc/nixos/configuration.nix | <syntaxhighlight lang="nix"># /etc/nixos/configuration.nix | ||
{ config, pkgs, lib, ... }: | { config, pkgs, lib, ... }: | ||
| Line 48: | Line 42: | ||
}; | }; | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
请查阅“man configuration.nix”文件以了解这些选项。重新构建系统以使这些选项生效: | |||
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight> | <syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight> | ||
列出构成系统闭包的所有存储路径并实例化它们: | |||
<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 | ||
<build output and list of successfully realised paths> | <build output and list of successfully realised paths> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
对您的用户和其他配置文件重复该步骤: | |||
<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> | ||
对于列在 ''/nix/var/nix/profiles/'' 或其子目录中的配置文件,可以忽略此警告。 | |||
有关更多信息,请参阅 nix-store 和 nix-instantiate 的手册。 | |||
<span id="Why_-_instead_of_-?"></span> | <span id="Why_-_instead_of_-?"></span> | ||
| Line 86: | Line 73: | ||
此外,由于开头部分长度一致,查看一系列软件包列表时在视觉上也更容易解析。 | 此外,由于开头部分长度一致,查看一系列软件包列表时在视觉上也更容易解析。 | ||
如果你仍然想知道原因,请在 shell 中运行 <code>ls -1 /nix/store | sort -R -t - -k 2 | less</code> 命令。''(? 不清楚)'' | |||
如果你不经常进行垃圾回收,或者正在测试编译变体,可能会发生这种情况: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/ | q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/ | ||
| Line 106: | Line 88: | ||
xvs7y09jf7j48p6l0p87iypgpq470jqw-nixos-build-vms/ | xvs7y09jf7j48p6l0p87iypgpq470jqw-nixos-build-vms/ | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<span id="I've_updated_my_channel_and_something_is_broken,_how_can_I_rollback_to_an_earlier_channel?"></span> | <span id="I've_updated_my_channel_and_something_is_broken,_how_can_I_rollback_to_an_earlier_channel?"></span> | ||
| Line 119: | Line 100: | ||
20 2014-08-12 19:09:20 (current) | 20 2014-08-12 19:09:20 (current) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
回滚到上一代的方法: | |||
<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> | ||
切换到指定世代: | |||
<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> | ||
< | <span id="I'm_working_on_a_new_package,_how_can_I_build_it_without_adding_it_to_nixpkgs?"></span> | ||
=== | === 我正在开发一个新软件包,如何在不将其添加到 nixpkgs 的情况下构建它? === | ||
<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> | ||
< | 如果您想在 64 位系统上测试,可以将 callPackage 替换为 callPackage_i686 来构建 32 位版本的软件包。 | ||
=== | |||
<span id="How_can_I_compile_a_package_with_debugging_symbols_included?"></span> | |||
=== 我如何编译包含调试符号的包?=== | |||
要使用 -Og 和 -g 参数构建软件包,且不去除调试符号,请使用: | |||
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage'</syntaxhighlight> | <syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage'</syntaxhighlight> | ||
另见 [[Debug Symbols]] | |||
< | <span id="How_can_I_force_a_rebuild_from_source_even_without_modifying_the_nix_expression?"></span> | ||
=== | === 即使不修改 nix 表达式,我如何强制从源代码重建?=== | ||
以 root 用户身份运行 nix-build 时,可以使用 --check 标志: | |||
<syntaxhighlight lang="bash">sudo nix-build --check -A ncdu</syntaxhighlight> | <syntaxhighlight lang="bash">sudo nix-build --check -A ncdu</syntaxhighlight> | ||
< | <span id="How_can_I_manage_software_with_nix-env_like_with_configuration.nix?"></span> | ||
=== 我如何使用 nix-env 管理软件,就像使用 configuration.nix 一样?=== | |||
有很多方法,其中一种如下: | |||
<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 195: | 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 lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 206: | Line 169: | ||
Another way is using [[Home Manager]]. | Another way is using [[Home Manager]]. | ||
</div> | </div> | ||
<span id="I've_downloaded_a_binary,_but_I_can'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"> | ||
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 224: | Line 189: | ||
</div> | </div> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
programs.nix-ld = { | programs.nix-ld = { | ||
| Line 230: | Line 194: | ||
libraries = [ pkgs.zlib pkgs.openssl ]; | libraries = [ pkgs.zlib pkgs.openssl ]; | ||
};</syntaxhighlight> | };</syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 240: | Line 203: | ||
</div> | </div> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
programs.nix-ld = { | programs.nix-ld = { | ||
| Line 246: | Line 208: | ||
libraries = pkgs.steam-run.args.multiPkgs pkgs; | libraries = pkgs.steam-run.args.multiPkgs pkgs; | ||
};</syntaxhighlight> | };</syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 256: | Line 217: | ||
</div> | </div> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
# mybinaryprogram.nix | # mybinaryprogram.nix | ||
| Line 277: | 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"> | ||
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"> | ||
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> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
# fhsUser.nix | # fhsUser.nix | ||
| Line 312: | 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"> | ||
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. | ||
</div> | </div> | ||
< | <span id="What_are_channels_and_how_do_they_get_updated?"></span> | ||
=== | === 什么是频道以及如何更新?=== | ||
{{main|Channel branches}} | {{main|Channel branches}} | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 366: | Line 331: | ||
</div> | </div> | ||
<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><span lang="en" dir="ltr" class="mw-content-ltr">Once the above download completes, the channel updates.</span></li> | ||
</ | </ol> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 376: | Line 341: | ||
</div> | </div> | ||
< | <span id="How_do_I_know_where's_nixpkgs_channel_located_and_at_which_commit?"></span> | ||
=== | === 我如何知道 nixpkgs 频道位于哪里以及在哪个提交?=== | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 387: | 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"> | ||
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"> | ||
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 410: | Line 377: | ||
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> | </div> | ||
<span id="There'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"> | <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> | </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'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"> | <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 lang="en" dir="ltr" class="mw-content-ltr"> | |||
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> | |||
<span id="Using_channels"></span> | |||
==== 使用频道 ==== | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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=" | {{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.}} | ||
</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"> | ||
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 437: | Line 515: | ||
</div> | </div> | ||
< | <span id="An_error_occurs_while_fetching_sources_from_an_url,_how_do_I_fix_it?"></span> | ||
=== | === 从 URL 获取源时发生错误,我该如何修复?=== | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 456: | 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"> | ||
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"> | ||
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"> | ||
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"> | ||
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> | ||
< | <span id="How_do_I_know_the_sha256_to_use_with_fetchgit,_fetchsvn,_fetchbzr_or_fetchcvs?"></span> | ||
=== | === 我如何知道要与 fetchgit、fetchsvn、fetchbzr 或 fetchcvs 一起使用的 sha256?=== | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 489: | Line 569: | ||
</div> | </div> | ||
<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 lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 497: | Line 575: | ||
</div> | </div> | ||
< | <span id="Should_I_use_http://hydra.nixos.org/_as_a_binary_cache?"></span> | ||
=== | === 我应该使用 http://hydra.nixos.org/ 作为二进制缓存吗?=== | ||
不可以。截至 2017 年,所有构建工件都直接推送到 http://cache.nixos.org/ 并在那里可用,因此将 http://hydra.nixos.org/ 设置为二进制缓存不再具有任何功能。 | |||
< | <span id="I'm_trying_to_install_NixOS_but_my_WiFi_isn't_working_and_I_don't_have_an_ethernet_port"></span> | ||
=== | === 我正在尝试安装 NixOS,但我的 WiFi 无法使用,而且我没有以太网端口 === | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 521: | Line 595: | ||
</div> | </div> | ||
< | <span id="How_can_I_disable_the_binary_cache_and_build_everything_locally?"></span> | ||
=== | === 我如何禁用二进制缓存并在本地构建所有内容?=== | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 533: | Line 606: | ||
</div> | </div> | ||
<syntaxhighlight lang="bash">nixos-rebuild switch --option binary-caches ''</syntaxhighlight> | <syntaxhighlight lang="bash">nixos-rebuild switch --option binary-caches ''</syntaxhighlight> | ||
< | <span id="How_do_I_enable_sandboxed_builds_on_non-NixOS?"></span> | ||
=== | === 如何在非 NixOS 上启用沙盒构建?=== | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 545: | Line 615: | ||
</div> | </div> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
# /etc/nix/nix.conf | # /etc/nix/nix.conf | ||
| Line 551: | 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> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
nix.settings.sandbox = true; | nix.settings.sandbox = true; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 564: | Line 633: | ||
</div> | </div> | ||
< | <span id="How_can_I_install_a_package_from_unstable_while_remaining_on_the_stable_channel?"></span> | ||
=== | === 我如何在稳定频道上安装来自不稳定频道的软件包?=== | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 572: | Line 640: | ||
</div> | </div> | ||
<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 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"> | ||
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"> | ||
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> | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ config, pkgs, ... }: | { config, pkgs, ... }: | ||
| Line 599: | Line 666: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<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 615: | 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 620: | 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> | ||
< | <span id="I'm_unable_to_connect_my_USB_HDD_|_External_HDD_is_failing_to_mount_automatically"></span> | ||
=== | === 我无法连接我的 USB HDD | 外部 HDD 无法自动安装 === | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 637: | Line 707: | ||
</div> | </div> | ||
<syntaxhighlight lang="bash">su nano /etc/nixos/configuration.nix</syntaxhighlight> | <syntaxhighlight lang="bash">su nano /etc/nixos/configuration.nix</syntaxhighlight> | ||
<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 lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 650: | Line 719: | ||
</div> | </div> | ||
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight> | <syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 658: | Line 725: | ||
</div> | </div> | ||
< | <span id="What_is_the_origin_of_the_name_"Nix""></span> | ||
=== | === “Nix” 这个名字的由来 === | ||
<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_"immutable""></span> | |||
=== NixOS 中的“不可变”是什么意思 === | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
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> | ||
< | <span id="I'm_getting_‘infinite_recursion’_errors_when_trying_to_do_something_clever_with_imports"></span> | ||
=== | === 当我尝试使用 <code>imports</code> 做一些巧妙的事情时,我得到了“无限递归”(infinite recursion)错误 === | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 691: | Line 758: | ||
</div> | </div> | ||
{{:FAQ/Libraries}} | {{:FAQ/Libraries}} | ||
{{:FAQ/nix-env -iA}} | {{:FAQ/nix-env -iA}} | ||
| Line 697: | Line 763: | ||
{{:FAQ/notfound}} | {{:FAQ/notfound}} | ||
<!-- Transclude subpages --> | <!-- Transclude subpages --> | ||
< | <span id="References"></span> | ||
== | == 参考 == | ||
[[Category:Cookbook]] | [[Category:Cookbook]] | ||