一个快速、友好且可靠的工具,帮助你在任何地方使用带有 Flakes 的 Nix。
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
nix-installer
已在多种环境中成功完成超过 2,000,000 次安装,包括 Github Actions 和 GitLab:
平台 | 多用户 | 仅 root | 成熟度 |
---|---|---|---|
Linux (x86_64 & aarch64) | ✓ (通过 systemd) | ✓ | 稳定 |
MacOS (x86_64 & aarch64) | ✓ | 稳定(见注释) | |
Valve Steam Deck (SteamOS) | ✓ | 稳定 | |
WSL2 (x86_64 & aarch64) | ✓ (通过 systemd) | ✓ | 稳定 |
Podman Linux 容器 | ✓ (通过 systemd) | ✓ | 稳定 |
Docker 容器 | ✓ | 稳定 |
[!注意] 仅在 MacOS 上,如果没有以图形方式登录的用户,删除用户和/或组可能会失败。
使用默认规划器和选项安装 Nix:
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
或者,自行下载特定平台的安装程序二进制文件:
$ curl -sL -o nix-installer https://install.determinate.systems/nix/nix-installer-x86_64-linux $ chmod +x nix-installer $ ./nix-installer
nix-installer
通过遵循由规划器制定的计划来安装 Nix。查看可用的规划器:
foo@ubuntuserver2204:~$ ./nix-installer install --help 使用规划器安装 Nix 默认情况下,会根据系统启发式地确定适当的规划器。 某些规划器有额外的选项,可以通过规划器的子命令设置。 用法:nix-installer install [选项] [计划] nix-installer install <命令> 命令: linux 适用于传统的、可变的 Linux 系统(如 Debian、RHEL 或 Arch)的规划器 steam-deck 适用于运行 SteamOS 的 Valve Steam Deck 的规划器 ostree 适用于使用 ostree 的不可变系统(如 Fedora Silverblue)的规划器 help 打印此消息或给定子命令的帮助 # ...
规划器有自己的选项和默认值,大多数是通用的:
$ ./nix-installer install linux --help 用于 Linux 安装的规划器 用法:nix-installer install linux [选项] 选项: # ... --nix-build-group-name <NIX_BUILD_GROUP_NAME> Nix 构建组名称 [环境变量: NIX_INSTALLER_NIX_BUILD_GROUP_NAME=] [默认值: nixbld] --nix-build-group-id <NIX_BUILD_GROUP_ID> Nix 构建组 GID [环境变量: NIX_INSTALLER_NIX_BUILD_GROUP_ID=] [默认值: 3000] # ...
规划器可以通过环境变量或命令参数进行配置:
$ curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | NIX_BUILD_GROUP_NAME=nixbuilder sh -s -- install --nix-build-group-id 4000 # 或者... $ NIX_BUILD_GROUP_NAME=nixbuilder ./nix-installer install --nix-build-group-id 4000
安装程序遇到问题? 请查阅我们的故障排除指南,看看是否涵盖了你的问题。
你可以通过运行以下命令将 Nix 升级到我们当前推荐的 Nix 版本:
sudo -i nix upgrade-nix
或者,你可以卸载并使用不同版本的 nix-installer
重新安装。
你可以通过运行以下命令删除由 nix-installer
安装的 Nix:
/nix/nix-installer uninstall
你可以像这样使用 nix-installer-action
Github Action:
on: pull_request: push: branches: [main] jobs: lints: name: Build runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 - name: 安装 Nix uses: DeterminateSystems/nix-installer-action@main - name: 运行 `nix build` run: nix build .
GitLab CI 运行器通常基于 Docker 并以 root
用户身份运行。这意味着 systemd
不存在,因此需要向 Linux 规划器传递 --init none
选项。
在默认的 GitLab.com 运行器上,可以像这样安装和使用 nix
:
test: script: - curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux --no-confirm --init none - . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh - nix run nixpkgs#hello - nix profile install nixpkgs#hello - hello
如果你使用的是不同的运行器,可能需要调整上述示例。
[!警告] 当使用
--init none
时,只有root
用户或能够提升到root
权限的用户才能运行 Nix:sudo -i nix run nixpkgs#hello
如果你不使用 systemd,你仍然可以通过明确指定 linux
计划和 --init none
来安装 Nix:
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux --init none
在没有初始化系统(如 systemd
)的 Docker/Podman 容器或 WSL2 实例中,请传递 --init none
。
对于容器(没有初始化系统):
[!警告] 当使用
--init none
时,只有root
用户或能够提升到root
权限的用户才能运行 Nix:sudo -i nix run nixpkgs#hello
# Dockerfile FROM ubuntu:latest RUN apt update -y RUN apt install curl -y RUN curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux \ --extra-conf "sandbox = false" \ --init none \ --no-confirm ENV PATH="${PATH}:/nix/var/nix/profiles/default/bin" RUN nix run nixpkgs#hello
docker build -t ubuntu-with-nix . docker run --rm -ti ubuntu-with-nix docker rmi ubuntu-with-nix # 或者 podman build -t ubuntu-with-nix . podman run --rm -ti ubuntu-with-nix podman rmi ubuntu-with-nix
对于带有 systemd 初始化的容器:
# Dockerfile FROM ubuntu:latest RUN apt update -y RUN apt install curl systemd -y RUN curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux \ --extra-conf "sandbox = false" \ --no-start-daemon \ --no-confirm ENV PATH="${PATH}:/nix/var/nix/profiles/default/bin" RUN nix run nixpkgs#hello CMD [ "/bin/systemd" ]
podman build -t ubuntu-systemd-with-nix . IMAGE=$(podman create ubuntu-systemd-with-nix) CONTAINER=$(podman start $IMAGE) podman exec -ti $CONTAINER /bin/bash podman rm -f $CONTAINER podman rmi $IMAGE
在某些容器工具(如 docker
)中,可以省略 sandbox = false
。省略它将对与 podman
等容器工具的兼容性产生负面影响。
我们强烈建议启用 systemd,然后正常安装 Nix:
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
如果启用了 WSLg,你可以在 Windows 的 Powershell 上打开 Linux 版 Firefox:
wsl nix run nixpkgs#firefox
要使用一些 OpenGL 应用程序,你可以使用 nixGL
(注意某些应用程序,如 blender
,可能无法工作):
wsl nix run --impure github:guibou/nixGL nix run nixpkgs#obs-studio
如果无法启用 systemd,请在命令末尾添加 --init none
:
[!警告] 当使用
--init none
时,只有root
用户或能够提升到root
权限的用户才能运行 Nix:sudo -i nix run nixpkgs#hello
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux --init none
如果你想跳过确认步骤,可以使用 --no-confirm
标志:
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm
这在非交互式脚本中使用安装程序时特别有用。
虽然 nix-installer
努力提供全面且无特殊情况的体验,但不幸的是,有些问题可能需要手动干预或操作者做出选择。
nix
时 nix-darwin
仍在安装的情况下使用 MacOS,网络请求失败如果之前卸载 nix
时没有先卸载 nix-darwin
,用户可能会遇到类似这样的错误:
$ nix shell nixpkgs#curl error: unable to download 'https://cache.nixos.org/g8bqlgmpa4yg601w561qy2n576i6g0vh.narinfo': Problem with the SSL CA cert (path? access rights?) (77)
这是因为 nix-darwin
提供了一个 org.nixos.activate-system
服务,该服务在 Nix 卸载后仍然存在。
在这种状态下,org.nixos.activate-system
服务与新安装的 Nix 交互,并将其使用的 SSL 证书更改为损坏的符号链接。
$ ls -lah /etc/ssl/certs total 0 drwxr-xr-x 3 root wheel 96B Oct 17 08:26 . drwxr-xr-x 6 root wheel 192B Sep 16 06:28 .. lrwxr-xr-x 1 root wheel 41B Oct 17 08:26 ca-certificates.crt -> /etc/static/ssl/certs/ca-certificates.crt
问题被进一步复杂化,因为在卸载 Nix 后,nix-darwin
卸载程序将无法工作,因为它使用 Nix 并需要网络连接。
可以通过删除 org.nixos.activate-system
服务和 ca-certificates
来解决这个问题:
$ sudo rm /Library/LaunchDaemons/org.nixos.activate-system.plist $ sudo launchctl bootout system/org.nixos.activate-system $ /nix/nix-installer uninstall $ sudo rm /etc/ssl/certs/ca-certificates.crt
然后重新运行 nix-installer
,应该就能正常工作了。
最新版本的 nix-installer
会拒绝卸载,除非先卸载 nix-darwin
,这有助于缓解这个问题。
由于你将使用 nix-installer
在没有 Nix 的系统上安装 Nix,默认构建是一个静态二进制文件。
在有 Nix 的系统上构建可移植的 Linux 二进制文件:
# 构建本地副本 nix build -L ".#nix-installer-static" # 构建远程主开发分 支 nix build -L "github:determinatesystems/nix-installer#nix-installer-static" # 构建特定版本的安装程序: export NIX_INSTALLER_TAG="v0.6.0" nix build -L "github:determinatesystems/nix-installer/$NIX_INSTALLER_TAG#nix-installer-static"
在 Mac 上:
# 构建本地副本 nix build -L ".#nix-installer" # 构建远程主开发分支 nix build -L "github:determinatesystems/nix-installer#nix-installer" # 构建特定版本的安装程序: export NIX_INSTALLER_TAG="v0.6.0" nix build -L "github:determinatesystems/nix-installer/$NIX_INSTALLER_TAG#nix-installer"
然后将 result/bin/nix-installer
复制到你想运行它的机器上。
你也可以通过 cargo
将 nix-installer
添加到没有 Nix 的系统中,无需担心系统依赖:
# 构建并运行本地副本 RUSTFLAGS="--cfg tokio_unstable" cargo run -- --help # 构建远程主开发分支 RUSTFLAGS="--cfg tokio_unstable" cargo install --git https://github.com/DeterminateSystems/nix-installer nix-installer --help # 构建特定版本的安装程序: export NIX_INSTALLER_TAG="v0.6.0" RUSTFLAGS="--cfg tokio_unstable" cargo install --git https://github.com/DeterminateSystems/nix-installer --tag $NIX_INSTALLER_TAG nix-installer --help
要使此构建可移植,请传递 --target x86_64-unknown-linux-musl
。
[!注意] 我们目前需要
--cfg tokio_unstable
,因为我们使用了 Tokio 的进程组,它包装了稳定的std
API,但由于需要提高 MSRV,所以不稳定。
[!警告] 作为库使用仍处于实验阶段。如果没有支持者,这个功能可能会在未来被移除。如果你正在使用这个功能,请告诉我们,我们可以制定稳定化路径。
将 nix-installer
添加到你的依赖项中:
cargo add nix-installer
如果你正在构建 CLI,请查看 cli
功能标志以获得 clap
集成。
你还需要编辑 .cargo/config.toml
以使用 tokio_unstable
,因为我们使用了 Tokio 的进程组,它包装了稳定的 std
API,但由于需要提高 MSRV,所以不稳定:
# .cargo/config.toml [build] rustflags=["--cfg", "tokio_unstable"]
你还需要设置 NIX_INSTALLER_TARBALL_PATH
环境变量,指向适合目标的 Nix 安装 tarball,如 nix-2.21.2-aarch64-darwin.tar.xz。
内容会被嵌入到生成的二进制文件中,而不是在安装时下载。
然后可以查看 文档:
cargo doc --open -p nix-installer
文档也可以通过 nix
构建:
nix build github:DeterminateSystems/nix-installer#nix-installer.doc firefox result-doc/nix-installer/index.html
对于希望固定版本的用户,可以在 curl
命令中指定要使用的 nix-installer
版本:
VERSION="v0.6.0" curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/${VERSION} | sh -s -- install
要发现可用的版本,或下载任何发布版本的二进制文件,请查看 Github 发布页面。
这些发布版本可以直接下载并使用:
VERSION="v0.6.0" ARCH="aarch64-linux" curl -sSf -L https://github.com/DeterminateSystems/nix-installer/releases/download/${VERSION}/nix-installer-${ARCH} -o nix-installer ./nix-installer install
每个安装程序版本都有一个相关的支持 nix 版本 -- 如果你固定安装程序版本,你也会间接固定到相关的 nix 版本。
你也可以通过 --nix-package-url
或 NIX_INSTALLER_NIX_PACKAGE_URL=
覆盖 nix
版本,但不建议这样做,因为我们没有测试过这种组合。
以下是一些包括 nix 版本、操作系统和架构的 nix
包 URL 示例:
与上游 Nix 安装脚本的不同之处:
nix.conf
中:
nix-command
和 flakes
功能bash-prompt-prefix