FAQ/zh: Difference between revisions

Ardenet (talk | contribs)
Created page with "=== 如何在非 NixOS 上启用沙盒构建?==="
Ardenet (talk | contribs)
Created page with "要使用 -Og 和 -g 参数构建软件包,且不去除调试符号,请使用:"
 
(18 intermediate revisions by 2 users not shown)
Line 43: Line 43:
}</syntaxhighlight>
}</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
请查阅“man configuration.nix”文件以了解这些选项。重新构建系统以使这些选项生效:
Check 'man configuration.nix' for these options. Rebuild for these options to take effect:
</div>


<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
列出构成系统闭包的所有存储路径并实例化它们:
List all store paths that form the system closure and realise them:
</div>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 60: Line 56:
</syntaxhighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
对您的用户和其他配置文件重复该步骤:
Repeat for your user and further profiles:
</div>


<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>


<div lang="en" dir="ltr" class="mw-content-ltr">
对于列在 ''/nix/var/nix/profiles/'' 或其子目录中的配置文件,可以忽略此警告。
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-store nix-instantiate 的手册。
Consult man pages of nix-store and nix-instantiate for further information.
</div>


<span id="Why_-_instead_of_-?"></span>
<span id="Why_-_instead_of_-?"></span>
Line 83: 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>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 129: Line 115:
</syntaxhighlight>
</syntaxhighlight>


<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>


<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>


<div lang="en" dir="ltr" class="mw-content-ltr">
如果您想在 64 位系统上测试,可以将 callPackage 替换为 callPackage_i686 来构建 32 位版本的软件包。
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>


<span id="How_can_I_compile_a_package_with_debugging_symbols_included?"></span>
<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>


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


<span id="How_can_I_force_a_rebuild_from_source_even_without_modifying_the_nix_expression?"></span>
<span id="How_can_I_force_a_rebuild_from_source_even_without_modifying_the_nix_expression?"></span>
=== 即使不修改 nix 表达式,我如何强制从源代码重建?===
=== 即使不修改 nix 表达式,我如何强制从源代码重建?===


<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>


<syntaxhighlight lang="bash">sudo nix-build --check -A ncdu</syntaxhighlight>
<syntaxhighlight lang="bash">sudo nix-build --check -A ncdu</syntaxhighlight>
Line 164: Line 141:
=== 我如何使用 nix-env 管理软件,就像使用 configuration.nix 一样?===
=== 我如何使用 nix-env 管理软件,就像使用 configuration.nix 一样?===


<div lang="en" dir="ltr" class="mw-content-ltr">
有很多方法,其中一种如下:
There are many ways, one is the following:
</div>


<ol style="list-style-type: decimal;">
<ol style="list-style-type: decimal;">
Line 412: Line 387:
<syntaxhighlight lang="command">
<syntaxhighlight lang="command">
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>
<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 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">
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">
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>
</syntaxhighlight>


Line 543: 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 607: Line 696:
</syntaxhighlight>
</syntaxhighlight>


<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 637: 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">
Line 645: Line 732:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="What_does_it_mean_to_say_that_NixOS_is_&quot;immutable&quot;"></span>
=== What does it mean to say that NixOS is "immutable" ===
=== NixOS 中的“不可变”是什么意思 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 653: Line 739:
</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">