FAQ/zh: Difference between revisions
No edit summary Tags: Mobile edit Mobile web edit |
Created page with "要使用 -Og 和 -g 参数构建软件包,且不去除调试符号,请使用:" |
||
| (28 intermediate revisions by 2 users not shown) | |||
| Line 3: | Line 3: | ||
为了避免重复回答那些经常被问到的问题以及处理新手常遇到的麻烦,这些问题被收录在这里。 | 为了避免重复回答那些经常被问到的问题以及处理新手常遇到的麻烦,这些问题被收录在这里。 | ||
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 45: | Line 43: | ||
}</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"> | ||
| Line 62: | Line 56: | ||
</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 85: | Line 73: | ||
此外,由于开头部分长度一致,查看一系列软件包列表时在视觉上也更容易解析。 | 此外,由于开头部分长度一致,查看一系列软件包列表时在视觉上也更容易解析。 | ||
如果你仍然想知道原因,请在 shell 中运行 <code>ls -1 /nix/store | sort -R -t - -k 2 | less</code> 命令。''(? 不清楚)'' | |||
如果你不经常进行垃圾回收,或者正在测试编译变体,可能会发生这种情况: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 117: | Line 101: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
回滚到上一代的方法: | |||
回滚到上一代的方法 | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 132: | Line 108: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
切换到指定世代: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 141: | Line 115: | ||
</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;"> | ||
| Line 210: | Line 170: | ||
</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"> | ||
| Line 327: | Line 286: | ||
</div> | </div> | ||
< | <span id="What_are_channels_and_how_do_they_get_updated?"></span> | ||
=== | === 什么是频道以及如何更新?=== | ||
{{main|Channel branches}} | {{main|Channel branches}} | ||
| Line 383: | 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 401: | Line 358: | ||
</div> | </div> | ||
< | <span id="Nixpkgs_branches"></span> | ||
=== Nixpkgs | === Nixpkgs 分支 === | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 422: | Line 378: | ||
</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"> | ||
| Line 433: | Line 388: | ||
NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/release-17.09.tar.gz nix-shell -p $software | NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/release-17.09.tar.gz nix-shell -p $software | ||
</syntaxhighlight> | </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"> | |||
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 lang="en" dir="ltr" class="mw-content-ltr"> | |||
{{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> | |||
<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> | |||
<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"> | <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> | </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"> | |||
Using this in <code>configuration.nix</code> then looks as follows: | |||
</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 446: | 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 490: | Line 558: | ||
</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 508: | 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 532: | 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 546: | Line 608: | ||
<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 572: | 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 636: | Line 696: | ||
</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 666: | 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"> | ||
| Line 674: | Line 732: | ||
</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"> | ||
| Line 682: | Line 739: | ||
</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 708: | Line 764: | ||
<!-- Transclude subpages --> | <!-- Transclude subpages --> | ||
< | <span id="References"></span> | ||
== | == 参考 == | ||
[[Category:Cookbook]] | [[Category:Cookbook]] | ||