mirror of
https://github.com/ryan4yin/nixos-and-flakes-book.git
synced 2024-11-22 08:03:26 +01:00
commit
f8001de437
4
.github/CONTRIBUTING.md
vendored
4
.github/CONTRIBUTING.md
vendored
@ -11,10 +11,10 @@ the preferred channel for bug reports, features requests and submitting pull req
|
||||
|
||||
But please respect the following restrictions:
|
||||
|
||||
* Please **do not** use the issue tracker for personal support requests (use
|
||||
- Please **do not** use the issue tracker for personal support requests (use
|
||||
[Stack Overflow](https://stackoverflow.com) or IRC)
|
||||
|
||||
* Please **do not** derail or troll issues. Keep the discussion on topic and
|
||||
- Please **do not** derail or troll issues. Keep the discussion on topic and
|
||||
respect the opinions of others
|
||||
|
||||
## License
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Advanced Topics
|
||||
# Advanced Topics
|
||||
|
||||
Once you're familiar with NixOS, you can explore Nix's three manuals and other documentation to discover more ways to use it:
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Debug with `nix repl`
|
||||
# Debug with `nix repl`
|
||||
|
||||
We've used `nix repl '<nixpkgs>'` many times to check the source code in this guide, it's a powerful tool to help us understand how things work in Nix.
|
||||
|
||||
|
@ -1,10 +1,8 @@
|
||||
## Best Practices
|
||||
|
||||
# Best Practices
|
||||
|
||||
Nix is powerful and flexible, it provides a lot of ways to do things, making it difficult to find the most suitable way to do your job.
|
||||
Here are some best practices that I've learned from the community, hope it can help you.
|
||||
|
||||
|
||||
## References
|
||||
|
||||
- [Tips&Tricks for NixOS Desktop - NixOS Discourse][Tips&Tricks for NixOS Desktop - NixOS Discourse]
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Remote deployment
|
||||
# Remote deployment
|
||||
|
||||
Some tools like [NixOps](https://github.com/NixOS/nixops), [deploy-rs](https://github.com/serokell/deploy-rs), and [colmena](https://github.com/zhaofengli/colmena) can all be used to deploy NixOS configuration to remote hosts, but they are all too complicated for me.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Run downloaded binaries on NixOS
|
||||
# Run downloaded binaries on NixOS
|
||||
|
||||
NixOS does not follow the FHS standard, so the binaries you download from the Internet will not likely work on NixOS. But there are some ways to make it work.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Simplify NixOS-related commands
|
||||
# Simplify NixOS-related commands
|
||||
|
||||
I use Makefile to simplify NixOS-related commands, which is very convenient.
|
||||
You can also use other similar tools to do this job, here I will only introduce my usage as a reference.
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Cross-platform Compilation
|
||||
# Cross-platform Compilation
|
||||
|
||||
On any Linux platform, there are two ways to do cross-platform compilation. For example, to build an `aarch64-linux` program on an `x86_64-linux` host, you can use the following methods:
|
||||
|
||||
@ -13,7 +13,7 @@ If you use method one, you don't need to enable `binfmt_misc`, but you need to e
|
||||
|
||||
If you use method two, you need to enable the `binfmt_misc` of the `aarch64` architecture in the NixOS configuration of the building machine.
|
||||
|
||||
### Cross Compilation
|
||||
## Cross Compilation
|
||||
|
||||
`nixpkgs` provides a set of predefined host platforms for cross-compilation called `pkgsCross`. You can explore them in `nix repl`.
|
||||
|
||||
@ -136,7 +136,7 @@ As for `flake.nix`, its setting method is very simple, even simpler than the set
|
||||
You do not need to add any additional modules, just specify `system` as `riscv64-linux`.
|
||||
Nix will automatically detect whether the current system is `riscv64-linux` during the build. If not, it will automatically build through the emulated system(QEMU). For users, these underlying operations are completely transparent.
|
||||
|
||||
### Custom build toolchain
|
||||
## Custom build toolchain
|
||||
|
||||
Sometimes we may need to use a custom toolchain for building, such as using our own gcc, or using our own musl libc, etc. This modification can be achieved through overlays.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Dev Environments
|
||||
# Dev Environments
|
||||
|
||||
We have learned how to build development environments, but it's a bit tedious to write `flake.nix` for each project.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Distributed Building
|
||||
# Distributed Building
|
||||
|
||||
Distributed building can speed up the build process by utilizing multiple machines.
|
||||
|
||||
@ -9,7 +9,7 @@ Distributed building is of great value in scenarios where there is no cache avai
|
||||
1. Users of `RISC-V` or `ARM64` architectures (especially `RISC-V`), because there are very few caches for these two architectures in the official cache repository, which often requires a lot of local compilation.
|
||||
2. Users who customize the system a lot, because the packages in the official cache repository are all default configurations. If you change the build parameters, then the official cache is not applicable, and you need to compile locally. For instance, in the embedded scenario, there is often a need for customization of the underlying kernel, drivers, etc., which leads to the need for local compilation.
|
||||
|
||||
### Configure Distributed Building
|
||||
## Configure Distributed Building
|
||||
|
||||
Currently, there is no official documentation for distributed building. However, I have listed some recommended reference documents at the end of this chapter, along with my distributed build configuration (a NixOS Module).
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Development Environments on NixOS
|
||||
# Development Environments on NixOS
|
||||
|
||||
NixOS's reproducibility makes it ideal for building development environments. However, if you're used to other distros, you may encounter problems because NixOS has its own logic. We'll explain this briefly below.
|
||||
|
||||
@ -8,7 +8,7 @@ You should NOT install the development environment of each language in the globa
|
||||
|
||||
In the following sections, we'll introduce how the development environment works in NixOS.
|
||||
|
||||
### Creating a Development Environment
|
||||
## Creating a Development Environment
|
||||
|
||||
We can create a development environment using `pkgs.mkShell { ... }` and open an interactive Bash shell of this development environment using `nix develop`.
|
||||
|
||||
@ -114,7 +114,7 @@ Here is a `flake.nix` that defined a development environment with nodejs 18 inst
|
||||
|
||||
Create an empty folder, save the above configuration as `flake.nix`, and then execute `nix develop` (or more precisely, you can use `nix develop .#default`), you will find that you have entered a nodejs 18 development environment, you can use `node` `npm` `pnpm` `yarn` and other commands. And when you just entered, `shellHook` was also executed, outputting the current version of nodejs.
|
||||
|
||||
### Enter the build environment of any Nix package
|
||||
## Enter the build environment of any Nix package
|
||||
|
||||
Now let's take a look at `nix develop`, first read the help document output by `nix develop --help`:
|
||||
|
||||
@ -242,7 +242,7 @@ Hello, world!
|
||||
|
||||
This usage is mainly used to debug the build process of a Nix package, or to execute some commands in the build environment of a Nix package.
|
||||
|
||||
### `nix shell` & `nix run`
|
||||
## `nix shell` & `nix run`
|
||||
|
||||
Compare to `nix develop`, these two commands are much simpler and easier to understand.
|
||||
|
||||
|
@ -4,17 +4,14 @@
|
||||
|
||||
TODO
|
||||
|
||||
### 1. stdenv
|
||||
|
||||
## 1. stdenv
|
||||
|
||||
TODO
|
||||
|
||||
### 2. language specific frameworks
|
||||
|
||||
## 2. language specific frameworks
|
||||
|
||||
TODO
|
||||
|
||||
|
||||
## References
|
||||
|
||||
- [NixOS Series 3: Software Packaging 101 - LanTian](https://lantian.pub/en/article/modify-computer/nixos-packaging.lantian/)
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Advantages & Disadvantages of NixOS
|
||||
# Advantages & Disadvantages of NixOS
|
||||
|
||||
## Advantages
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Introduction to Nix & NixOS
|
||||
# Introduction to Nix & NixOS
|
||||
|
||||
Nix is a declarative package manager, that allows users to declare the expected system state in some configuration files, and Nix is responsible for achieving that goal.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Installation
|
||||
# Installation
|
||||
|
||||
Nix can be installed in multiple ways:
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Downgrading or Upgrading Packages
|
||||
# Downgrading or Upgrading Packages
|
||||
|
||||
After using Flakes, most people are currently using the `nixos-unstable` branch of nixpkgs. However, sometimes you may encounter bugs, such as the [chrome/vscode crash problem](https://github.com/swaywm/sway/issues/7562).
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Get Started with NixOS
|
||||
# Get Started with NixOS
|
||||
|
||||
After learning the basics of the Nix language, we can start using it to configure our NixOS. The default configuration for NixOS is located at `/etc/nixos/configuration.nix`, which contains all the declarative configuration for the system, such as time zone, language, keyboard layout, network, users, file system, boot options, etc.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Introduction to Flakes
|
||||
# Introduction to Flakes
|
||||
|
||||
The flakes experimental feature is a major development for Nix, it introduces a policy for managing dependencies between Nix expressions, it improves reproducibility, composability and usability in the Nix ecosystem. Although it's still an experimental feature, flakes have been widely used by the Nix community.[^1]
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Modularize Your NixOS Configuration
|
||||
# Modularize Your NixOS Configuration
|
||||
|
||||
At this point, the skeleton of the entire system is configured. The current configuration structure in `/etc/nixos` should be:
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
## NixOS with Flakes Enabled
|
||||
# NixOS with Flakes Enabled
|
||||
|
||||
### Enabling Flakes Support
|
||||
## Enabling Flakes Support
|
||||
|
||||
Flakes provide better reproducibility and a clearer package structure that is easier to maintain compared to the default configuration approach of NixOS. Therefore, it's recommended to manage NixOS with Flakes.
|
||||
|
||||
@ -38,7 +38,7 @@ However, as Flakes is still an experimental feature, it's not enabled by default
|
||||
|
||||
To apply the changes, run `sudo nixos-rebuild switch`. After that, you can write the configuration for NixOS with Flakes.
|
||||
|
||||
### Switching System Configuration to `flake.nix`
|
||||
## Switching System Configuration to `flake.nix`
|
||||
|
||||
After enabling `flakes`, `sudo nixos-rebuild switch` will first try to read `/etc/nixos/flake.nix` every time you run it. If not found, it will fallback to `/etc/nixos/configuration.nix`.
|
||||
|
||||
@ -135,7 +135,7 @@ We defined a NixOS system called `nixos-test` with a configuration file at `./co
|
||||
|
||||
To apply the configuration, run `sudo nixos-rebuild switch`. No changes will be made to the system because we imported the old configuration file in `/etc/nixos/flake.nix`, so the actual state we declared remains unchanged.
|
||||
|
||||
### Manage System Software through Flakes
|
||||
## Manage System Software through Flakes
|
||||
|
||||
After the switch, we can manage the system through Flakes. The most common requirement for managing a system is to install packages. We have seen how to install packages through `environment.systemPackages` before, and these packages are all from the official nixpkgs repository.
|
||||
|
||||
@ -199,7 +199,7 @@ Then update `configuration.nix` to install `helix` from the input `helix`:
|
||||
|
||||
To deploy the changes, run `sudo nixos-rebuild switch`. Then start the Helix editor by running the `helix` command.
|
||||
|
||||
### Add Custom Cache Mirror
|
||||
## Add Custom Cache Mirror
|
||||
|
||||
> If you don't need to customize the cache mirror, you can safely skip this section.
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
## Other useful Tips
|
||||
# Other useful Tips
|
||||
|
||||
|
||||
### Manage the configuration with Git
|
||||
## Manage the configuration with Git
|
||||
|
||||
NixOS configuration is just a set of text files, it is very suitable to be managed with Git, and thus we can easily rollback to a previous version when we encounter some problems.
|
||||
|
||||
@ -42,7 +41,7 @@ sudo nixos-rebuild switch --flake .#nixos-test
|
||||
|
||||
More operations on Git are not described here. Generally speaking, rollback can be done directly through Git. Only when the system crashes completely, you will need to restart into bootloader and boot the system from the previous historical version.
|
||||
|
||||
### View and delete history data {#view-and-delete-history}
|
||||
## View and delete history data {#view-and-delete-history}
|
||||
|
||||
As we mentioned before, each deployment of NixOS will generate a new version, and all versions will be added to the system boot options. In addition to restarting the computer, we can also query all available historical versions through the following command:
|
||||
|
||||
@ -66,7 +65,6 @@ Another command returns all packages installed in the system:
|
||||
nix-env -qa
|
||||
```
|
||||
|
||||
|
||||
## Reduce Disk Usage
|
||||
|
||||
The following configuration can be used to reduce disk usage, feel free to add it into your NixOS Configuration.
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Getting Started with Home Manager
|
||||
# Getting Started with Home Manager
|
||||
|
||||
As I mentioned earlier, NixOS can only manage system-level configuration. To manage user-level configuration in the Home directory, we need to install Home Manager.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Updating the System
|
||||
# Updating the System
|
||||
|
||||
With Flakes, updating the system is simple. Just run the following commands in `/etc/nixos`:
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## pkgs.callPackage
|
||||
# pkgs.callPackage
|
||||
|
||||
In the previous section, we used `import xxx.nix` to import Nix files multiple times. This syntax simply returns the execution result of the file without any further processing.
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
## Nixpkgs's Advanced Usage
|
||||
|
||||
# Nixpkgs's Advanced Usage
|
||||
|
||||
`callPackage`, `Overriding`, and `Overlays` are the techniques occasionally used when using Nix to customize the build method of Nix packages.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Overlays
|
||||
# Overlays
|
||||
|
||||
The `override` we introduced earlier generates a new Derivation that does not affect the original Derivation in `pkgs`. It's only suitable for use as a local parameter. If you need to override a Derivation that is also dependent on other Nix packages, then other Nix packages will still use the original Derivation.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Overriding
|
||||
# Overriding
|
||||
|
||||
Simply put, all Nix packages in nixpkgs can be customized with `<pkg>.override {}` to define some build parameters, which returns a new Derivation that uses custom parameters. For example:
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Flake inputs {#flake-inputs}
|
||||
# Flake inputs {#flake-inputs}
|
||||
|
||||
The `inputs` in `flake.nix` is an attribute set, used to specify the dependencies of the current flake, there are many types of `inputs`, for example:
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
## Other Usage of Flakes
|
||||
# Other Usage of Flakes
|
||||
|
||||
Up to now, we have written a lot of configurations with Flakes to manage NixOS. Here is a brief introduction to the more detailed content of the Flakes, as well as the new command lines commonly used with flakes.
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## Flake outputs
|
||||
# Flake outputs
|
||||
|
||||
the `outputs` in `flake.nix` are what a flake produces as part of its build. Each flake can have many different outputs simultaneously, including but not limited to:
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Usage of The New CLI
|
||||
# Usage of The New CLI
|
||||
|
||||
after enabling `nix-command` & `flake`, you can use `nix help` to get all the info of [New Nix Commands][New Nix Commands], some useful examples are listed below:
|
||||
|
||||
@ -20,7 +20,7 @@ nix develop "github:DeterminateSystems/zero-to-nix#example"
|
||||
|
||||
# instead of using a remote flake, you can open a bash shell using the flake located in the current directory.
|
||||
mkdir my-flake && cd my-flake
|
||||
## init a flake with template
|
||||
# init a flake with template
|
||||
nix flake init --template "github:DeterminateSystems/zero-to-nix#javascript-dev"
|
||||
# open a bash shell using the flake in current directory
|
||||
nix develop
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## Basics of The Nix language
|
||||
# Basics of The Nix language
|
||||
|
||||
The Nix language is used to declare the configuration to be built by Nix, if you want to play with NixOS and Flakes and enjoy the benefits they bring, you must learn the basics of this language first.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 进阶玩法 {#advanced-topics}
|
||||
# 进阶玩法 {#advanced-topics}
|
||||
|
||||
逐渐熟悉 Nix 这一套工具链后,可以进一步读一读 Nix 的三本手册,挖掘更多的玩法:
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## 通过 `nix repl` 查看源码、调试配置 {#debug-via-nix-repl}
|
||||
# 通过 `nix repl` 查看源码、调试配置
|
||||
|
||||
前面我们已经使用 `nix repl '<nixpkgs>'` 看过很多次源码了,这是一个非常强大的工具,可以帮助我们理解 Nix 的工作原理。
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## 最佳实践 {#best-practices}
|
||||
# 最佳实践
|
||||
|
||||
Nix 非常强大且灵活,做一件事有非常多的方法,这就导致了很难找到最合适的方法来做你的工作。
|
||||
这里记录了一些我在使用 NixOS 中学习到的最佳实践,希望能帮到你。
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## 远程部署
|
||||
# 远程部署
|
||||
|
||||
社区的一些工具,比如 [NixOps](https://github.com/NixOS/nixops), [deploy-rs](https://github.com/serokell/deploy-rs), 跟 [colmena](https://github.com/zhaofengli/colmena),都可以用来部署 NixOS 配置到远程主机,但是都太复杂了,所以先全部跳过。
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 运行非 NixOS 的二进制文件 {#run-non-nixos-binaries}
|
||||
# 运行非 NixOS 的二进制文件
|
||||
|
||||
NixOS 不遵循 FHS 标准,因此你从网上下载的二进制程序在 NixOS 上大概率是跑不了的。
|
||||
为了在 NixOS 上跑这些非 NixOS 的二进制程序,需要做一些骚操作。有位老兄在这里总结了 10 种实现此目的的方法:[Different methods to run a non-nixos executable on Nixos](https://unix.stackexchange.com/questions/522822/different-methods-to-run-a-non-nixos-executable-on-nixos),推荐一读。
|
||||
@ -55,5 +55,4 @@ $ fhs
|
||||
|
||||
- [Tips&Tricks for NixOS Desktop - NixOS Discourse][Tips&Tricks for NixOS Desktop - NixOS Discourse]: Just as the title says, it is a collection of tips and tricks for NixOS desktop.
|
||||
|
||||
|
||||
[Tips&Tricks for NixOS Desktop - NixOS Discourse]: https://discourse.nixos.org/t/tips-tricks-for-nixos-desktop/28488
|
@ -1,5 +1,4 @@
|
||||
|
||||
## 使用 Makefile 简化 NixOS 相关命令
|
||||
# 使用 Makefile 简化 NixOS 相关命令
|
||||
|
||||
> 注意: Makefile 的 target 名称不能与当前目录下的文件或者目录重名,否则 target 将不会被执行!
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 跨平台编译
|
||||
# 跨平台编译
|
||||
|
||||
首先在任何 Linux 平台上,都有两种方法进行跨平台构建。
|
||||
以在 x86_64 架构上构建 aarch64 架构程序为例,两种构建方法说明如下:
|
||||
@ -14,7 +14,7 @@
|
||||
|
||||
如果使用方法二,就不需要启用 binfmt_misc 了,但是需要通过交叉编译工具链来执行编译。
|
||||
|
||||
### 交叉编译
|
||||
## 交叉编译
|
||||
|
||||
nixpkgs 包含了一系列预定义好的交叉编译工具链,其名为 `pkgsCross`,我们先通过 `nix repl` 来看看有哪些工具链:
|
||||
|
||||
@ -94,8 +94,7 @@ pkgsCross.mmix
|
||||
|
||||
模块中的 `nixpkgs.crossSystem` 参数用于将 `pkgs` 设置为交叉编译工具链,这样构建出的内容全都会是 `riscv64-linux` 架构的。
|
||||
|
||||
|
||||
### 通过模拟系统进行跨平台编译
|
||||
## 通过模拟系统进行跨平台编译
|
||||
|
||||
第二种方法是通过模拟系统进行跨平台编译,这种方法不需要交叉编译工具链。
|
||||
|
||||
@ -138,7 +137,7 @@ pkgsCross.mmix
|
||||
可以看到我们未添加任何额外的模块,仅仅是指定了 `system` 为 `riscv64-linux`.
|
||||
Nix 在构建时会自动检测当前系统是否为 `riscv64-linux`,如果不是,它会自动通过 QEMU 模拟系统进行构建,对用户而言这些底层操作完全是透明的。
|
||||
|
||||
#### Linux binfmt_misc
|
||||
## Linux binfmt_misc
|
||||
|
||||
前面只说了怎么用,如果你想了解更底层的细节,这里也简单介绍一下。
|
||||
|
||||
@ -163,8 +162,7 @@ Linux 4.8 中新增“F”(fix binary,固定二进制)标志位,使 moun
|
||||
|
||||
总的来说,比起一般情况显式调用解释器去执行非原生架构程序,binfmt_misc 产生的一个重要意义在于透明性。有了 binfmt_misc 后,用户在执行程序时不需要再关心要用什么解释器去执行,好像任何架构的程序都能够直接执行一样,而可配置的“F”标志位更是锦上添花,使解释器程序在安装时立即就被加载进内存,后续的环境改变也不会影响执行过程。
|
||||
|
||||
|
||||
### 自定义构建工具链
|
||||
## 自定义构建工具链
|
||||
|
||||
有时候我们会需要使用自定义的工具链进行构建,比如使用自己编译的 gcc,或者使用自己编译的 musl libc 等等,这种修改可以通过 overlays 来实现。
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Dev Environments
|
||||
# Dev Environments
|
||||
|
||||
前面我们已经学习了构建开发环境的实现原理,但是每次都要自己写一堆重复性较高的 `flake.nix`,略显繁琐。
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 分布式构建
|
||||
# 分布式构建
|
||||
|
||||
分布式构建可以通过多台机器来分担本地的编译压力,加快构建速度。
|
||||
|
||||
@ -10,7 +10,7 @@ NixOS 官方的 cache.nixos.org 中提供了绝大多数 X86_64 架构的缓存
|
||||
2. 对系统进行大量定制的用户,因为官方缓存仓库中的 packages 都是默认配置,如果你改了构建参数,那么官方缓存就不适用了,这时候就需要本地编译。
|
||||
1. 比如嵌入式场景下往往对底层内核、驱动等有定制需求,导致需要本地编译。
|
||||
|
||||
### 配置分布式构建
|
||||
## 配置分布式构建
|
||||
|
||||
官方没有详细文档讲这个,我在文末列出了一些建议阅读的参考文档,同时如下是我的分布式构建配置(一个 NixOS Module):
|
||||
|
||||
@ -137,7 +137,6 @@ NixOS 官方的 cache.nixos.org 中提供了绝大多数 X86_64 架构的缓存
|
||||
3. 多机远程构建是以 Derivation 为单位的,因此在构建一些比较大的包时,其他机器可能会空闲很久,一直等这个大包构建完毕,这导致了资源的浪费。
|
||||
1. 在构建的 packages 较多并且可以并行执行时,可以轻松将所有主机的 CPU 都用上,这确实非常爽。
|
||||
|
||||
|
||||
## References
|
||||
|
||||
- [Distributed build - NixOS Wiki](https://nixos.wiki/wiki/Distributed_build)
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 在 NixOS 上进行开发工作
|
||||
# 在 NixOS 上进行开发工作
|
||||
|
||||
由于 NixOS 自身可复现的特性,它非常适合用于搭建开发环境。
|
||||
但是如果你想直接将在其他发行版上的环境搭建经验用在 NixOS 上,可能会遇到许多问题,因为 NixOS 有自己的一套逻辑在,下面我们先对此稍作说明。
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
在本章中我们先学习一下 Nix Flakes 开发环境的实现原理,后面的章节再按使用场景介绍一些更具体的内容。
|
||||
|
||||
### 创建与使用开发环境
|
||||
## 创建与使用开发环境
|
||||
|
||||
在 Nix Flakes 中,我们可以通过 `pkgs.mkShell { ... }` 来定义一个项目环境,通过 `nix develop` 来打开一个该开发环境的交互式 Bash Shell.
|
||||
|
||||
@ -118,7 +118,7 @@ stdenv.mkDerivation ({
|
||||
|
||||
建个空文件夹,将上面的配置保存为 `flake.nix`,然后执行 `nix develop`(或者更精确点,可以用 `nix develop .#default`),你会发现你已经进入了一个 nodejs 18 的开发环境,可以使用 `node` `npm` `pnpm` `yarn` 等命令了。而且刚进入时,`shellHook` 也被执行了,输出了当前 nodejs 的版本。
|
||||
|
||||
### 进入任何 Nix 包的构建环境
|
||||
## 进入任何 Nix 包的构建环境
|
||||
|
||||
现在再来看看 `nix develop`,先读下 `nix develop --help` 输出的帮助文档:
|
||||
|
||||
@ -246,7 +246,7 @@ Hello, world!
|
||||
|
||||
这种用法的主要应用场景是调试某个 Nix 包的构建过程,或者在某个 Nix 包的构建环境中执行一些命令。
|
||||
|
||||
### `nix shell` 与 `nix run`
|
||||
## `nix shell` 与 `nix run`
|
||||
|
||||
与 `nix develop` 相比,这两个命令就简单且好理解很多了。
|
||||
|
||||
@ -287,7 +287,6 @@ Hello, world!
|
||||
|
||||
比如说我们上面测试的包 hello,`nix run` 实际会执行 `$out/bin/hello` 这个程序。
|
||||
|
||||
|
||||
## References
|
||||
|
||||
- [pkgs.mkShell - nixpkgs manual](https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell)
|
||||
|
@ -2,17 +2,14 @@
|
||||
|
||||
TODO
|
||||
|
||||
### 1. stdenv 构建介绍
|
||||
|
||||
## 1. stdenv 构建介绍
|
||||
|
||||
TODO
|
||||
|
||||
### 2. language specific frameworks
|
||||
|
||||
## 2. language specific frameworks
|
||||
|
||||
TODO
|
||||
|
||||
|
||||
## References
|
||||
|
||||
- [NixOS 系列(三):软件打包,从入门到放弃 - LanTian](https://lantian.pub/article/modify-computer/nixos-packaging.lantian/)
|
||||
|
@ -1,5 +1,4 @@
|
||||
## NixOS 的优缺点
|
||||
|
||||
# NixOS 的优缺点
|
||||
|
||||
## 优点 {#advantages}
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## Nix 与 NixOS 简介
|
||||
# Nix 与 NixOS 简介
|
||||
|
||||
Nix 是一个声明式的软件包管理器,用户需要通过某些配置声明好期望的环境状态,而 Nix 负责达成这个目标。
|
||||
|
||||
@ -17,8 +16,6 @@ Nix 是一个声明式的软件包管理器,用户需要通过某些配置声
|
||||
|
||||
因为 Nix 声明式、可复现的特性,Nix 不仅可用于管理桌面电脑的环境,也有很多人用它管理开发编译环境、云上虚拟机、容器镜像构建等等,Nix 官方的 [NixOps](https://github.com/NixOS/nixops) 与社区的 [deploy-rs](https://github.com/serokell/deploy-rs) 都是基于 Nix 实现的运维工具。
|
||||
|
||||
|
||||
|
||||
## 为什么选择 NixOS?
|
||||
|
||||
好几年前就听说过 Nix 包管理器,它用 Nix 语言编写配置来管理系统依赖,此外基于 Nix 包管理器设计的 Linux 发行版 NixOS,还能随时将系统回滚到任一历史状态(额实际上这个回滚有些限制,后面会介绍)。 虽然听着很牛,但是不仅要多学一门语言,装个包还得写代码,当时觉得太麻烦就没研究。
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 安装 NixOS
|
||||
# 安装 NixOS
|
||||
|
||||
Nix 有多种安装方式:
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## 降级与升级软件包 {#rollback-package-version}
|
||||
# 降级与升级软件包 {#rollback-package-version}
|
||||
|
||||
在使用 Nix Flakes 后,目前大家用得比较多的都是 `nixos-unstable` 分支的 nixpkgs,有时候就会遇到一些 bug,比如我最近(2023/5/6)就遇到了 [chrome/vscode 闪退的问题](https://github.com/swaywm/sway/issues/7562)。
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 开始使用 NixOS
|
||||
# 开始使用 NixOS
|
||||
|
||||
了解了 Nix 语言的基本用法之后,我们就可以开始使用 Nix 语言来配置 NixOS 系统了。
|
||||
NixOS 的系统配置路径为 `/etc/nixos/configuration.nix`,它包含系统的所有声明式配置,如时区、语言、键盘布局、网络、用户、文件系统、启动项、桌面环境等等。
|
||||
@ -74,7 +74,6 @@ NixOS 的系统配置路径为 `/etc/nixos/configuration.nix`,它包含系统
|
||||
- 系统级别的配置,可以考虑在 [Configuration - NixOS Manual](https://nixos.org/manual/nixos/unstable/index.html#ch-configuration) 找找相关文档
|
||||
- 直接在 [nixpkgs](https://github.com/NixOS/nixpkgs) 仓库中搜索关键字,读相关的源码。
|
||||
|
||||
|
||||
## 参考
|
||||
|
||||
- [Overview of the NixOS Linux distribution](https://nixos.wiki/wiki/Overview_of_the_NixOS_Linux_distribution)
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Flakes 简介
|
||||
# Flakes 简介
|
||||
|
||||
Flakes 实验特性是 Nix 项目的一项重大进展,它引入了一种管理 Nix 表达式之间的依赖关系的策略,提高了 Nix 生态系统中的可复现性、可组合性和可用性。
|
||||
虽然 Flakes 仍然是一个试验性的功能,但已经被 Nix 社区广泛采用。[^1]
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 模块化 NixOS 配置 {#modularize-nixos-configuration}
|
||||
# 模块化 NixOS 配置 {#modularize-nixos-configuration}
|
||||
|
||||
到这里整个系统的骨架基本就配置完成了,当前我们 `/etc/nixos` 中的系统配置结构应该如下:
|
||||
|
||||
@ -246,7 +246,6 @@ nix-repl> outputs.nixosConfigurations.nixos-test.config.environment.systemPackag
|
||||
|
||||
> 虽然单纯调整 `systemPackages` 的顺序没什么用,但是在其他地方可能会有用...
|
||||
|
||||
|
||||
## References
|
||||
|
||||
- [Nix modules: Improving Nix's discoverability and usability ](https://cfp.nixcon.org/nixcon2020/talk/K89WJY/)
|
||||
|
@ -1,5 +1,4 @@
|
||||
## 使用 Flakes 来管理你的 NixOS
|
||||
|
||||
# 使用 Flakes 来管理你的 NixOS
|
||||
|
||||
## 启用 NixOS 的 Flakes 支持 {#enable-nix-flakes}
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
## NixOS 的其他实用技巧
|
||||
|
||||
# NixOS 的其他实用技巧
|
||||
|
||||
## 使用 Git 管理 NixOS 配置 {#git-manage-nixos-config}
|
||||
|
||||
@ -63,7 +62,7 @@ sudo nix store gc --debug
|
||||
nix-env -qa
|
||||
```
|
||||
|
||||
## 节约存储空间
|
||||
# 节约存储空间
|
||||
|
||||
如下配置可以比较好的缩减 NixOS 的磁盘占用,可以考虑将它们添加到你的 NixOS 配置中:
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## 安装使用 Home Manager
|
||||
# 安装使用 Home Manager
|
||||
|
||||
前面简单提过,NixOS 自身的配置文件只能管理系统级别的配置,而用户级别的配置则需要使用 home-manager 来管理。
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## 更新系统 {#update-nixos-system}
|
||||
# 更新系统 {#update-nixos-system}
|
||||
|
||||
在使用了 Nix Flakes 后,要更新系统也很简单,先更新 flake.lock 文件,然后部署即可。在配置文件夹中执行如下命令:
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
## pkgs.callPackage
|
||||
|
||||
# pkgs.callPackage
|
||||
|
||||
前面我们介绍并大量使用了 `import xxx.nix` 来导入 Nix 文件,这种语法只是单纯地返回该文件的执行结果,不会对该结果进行进一步处理。
|
||||
比如说 `xxx.nix` 的内容是形如 `{...}: {...}`,那么 `import xxx.nix` 的结果就是该文件中定义的这个函数。
|
||||
@ -65,7 +64,6 @@
|
||||
|
||||
就如上面所展示的,通过 `pkgs.callPackage` 我们可以给 `kernel.nix` 定义的函数传入不同的 `src` 与 `boardName`,来生成不同的内核包,这样就可以使用同一份 `kernel.nix` 来适配不同的内核源码与不同的开发板了。
|
||||
|
||||
|
||||
## 参考
|
||||
|
||||
- [Chapter 13. Callpackage Design Pattern - Nix Pills](https://nixos.org/guides/nix-pills/callpackage-design-pattern.html)
|
@ -1,5 +1,4 @@
|
||||
|
||||
## Nixpkgs 的高级用法 {#nixpkgs-advanced-usage}
|
||||
# Nixpkgs 的高级用法 {#nixpkgs-advanced-usage}
|
||||
|
||||
callPackage、Overriding 与 Overlays 是在使用 Nix 时偶尔会用到的技术,它们都是用来自定义 Nix 包的构建方法的。
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
## Overlays
|
||||
# Overlays
|
||||
|
||||
前面介绍的 override 函数都会生成新的 Derivation,不影响 pkgs 中原有的 Derivation,只适合作为局部参数使用。
|
||||
但如果你需要覆写的 Derivation 还被其他 Nix 包所依赖,那其他 Nix 包使用的仍然会是原有的 Derivation.
|
||||
@ -84,7 +84,7 @@ args:
|
||||
|
||||
```nix
|
||||
# 为了不使用默认的 rime-data,改用我自定义的小鹤音形数据,这里需要 override
|
||||
# 参考 https://github.com/NixOS/nixpkgs/blob/e4246ae1e7f78b7087dce9c9da10d28d3725025f/pkgs/tools/inputmethods/fcitx5/fcitx5-rime.nix
|
||||
## 参考 https://github.com/NixOS/nixpkgs/blob/e4246ae1e7f78b7087dce9c9da10d28d3725025f/pkgs/tools/inputmethods/fcitx5/fcitx5-rime.nix
|
||||
{pkgs, config, lib, ...}:
|
||||
|
||||
(self: super: {
|
||||
@ -167,7 +167,6 @@ args:
|
||||
|
||||
你可以在我的配置仓库 [ryan4yin/nix-config/v0.0.4](https://github.com/ryan4yin/nix-config/tree/v0.0.4) 查看更详细的内容,获取些灵感。
|
||||
|
||||
|
||||
## 参考
|
||||
|
||||
- [Chapter 3. Overlays - nixpkgs Manual](https://nixos.org/manual/nixpkgs/stable/#chap-overlays)
|
@ -1,5 +1,4 @@
|
||||
## Overriding
|
||||
|
||||
# Overriding
|
||||
|
||||
简单的说,所有 nixpkgs 中的 Nix 包都可以通过 `<pkg>.override {}` 来自定义某些构建参数,它返回一个使用了自定义参数的新 Derivation. 举个例子:
|
||||
|
||||
@ -87,7 +86,6 @@ helloWithDebug = pkgs.hello.overrideAttrs (finalAttrs: previousAttrs: {
|
||||
|
||||
具体的内部参数可以通过 `nix repl '<nixpkgs>'` 然后输入 `:e stdenv.mkDerivation` 来查看其源码。
|
||||
|
||||
|
||||
## 参考
|
||||
|
||||
- [Chapter 4. Overriding - nixpkgs Manual](https://nixos.org/manual/nixpkgs/stable/#chap-overrides)
|
@ -1,5 +1,4 @@
|
||||
|
||||
## Flake 的 inputs {#flake-inputs}
|
||||
# Flake 的 inputs {#flake-inputs}
|
||||
|
||||
`flake.nix` 中的 `inputs` 是一个 attribute set,用来指定当前 Flake 的依赖,inputs 有很多种类型,举例如下:
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
|
||||
## Nix Flakes 的其他玩法 {#nix-flakes-usage}
|
||||
# Nix Flakes 的其他玩法 {#nix-flakes-usage}
|
||||
|
||||
到这里我们已经写了不少 Nix Flakes 配置来管理 NixOS 系统了,这里再简单介绍下 Nix Flakes 更细节的内容,以及常用的 nix flake 命令。
|
||||
|
||||
此外 [Zero to Nix - Determinate Systems][Zero to Nix - Determinate Systems] 是一份全新的 Nix & Flake 新手入门文档,写得比较浅显易懂,也可以一读。
|
||||
|
||||
|
||||
[Zero to Nix - Determinate Systems]: https://github.com/DeterminateSystems/zero-to-nix
|
@ -1,6 +1,4 @@
|
||||
|
||||
|
||||
## Flake 的 outputs {#flake-outputs}
|
||||
# Flake 的 outputs {#flake-outputs}
|
||||
|
||||
`flake.nix` 中的 `outputs` 是一个 attribute set,是整个 Flake 的构建结果,每个 Flake 都可以有许多不同的 outputs。
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
## 新一代 Nix 命令行工具的使用 {#flake-commands-usage}
|
||||
# 新一代 Nix 命令行工具的使用 {#flake-commands-usage}
|
||||
|
||||
在启用了 `nix-command` & `flakes` 功能后,我们就可以使用 Nix 提供的新一代 Nix 命令行工具 [New Nix Commands][New Nix Commands] 了,下面列举下其中常用命令的用法:
|
||||
|
||||
@ -22,9 +21,9 @@ nix develop "github:DeterminateSystems/zero-to-nix#example"
|
||||
|
||||
# 除了使用远程 flake uri 之外,你也可以使用当前目录下的 flake 来创建一个开发环境。
|
||||
mkdir my-flake && cd my-flake
|
||||
## 通过模板初始化一个 flake
|
||||
# 通过模板初始化一个 flake
|
||||
nix flake init --template "github:DeterminateSystems/zero-to-nix#javascript-dev"
|
||||
## 使用当前目录下的 flake 创建一个开发环境,并打开一个 bash shell
|
||||
# 使用当前目录下的 flake 创建一个开发环境,并打开一个 bash shell
|
||||
nix develop
|
||||
# 或者如果你的 flake 有多个 devShell 输出,你可以指定使用名为 example 的那个
|
||||
nix develop .#example
|
||||
@ -36,5 +35,4 @@ nix build "nixpkgs#bat"
|
||||
# 构建一个本地 flake 和 nix develop 是一样的,不再赘述
|
||||
```
|
||||
|
||||
|
||||
[New Nix Commands]: https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html
|
@ -1,4 +1,4 @@
|
||||
## Nix 语言入门
|
||||
# Nix 语言入门
|
||||
|
||||
|
||||
Nix 语言是 Nix 包管理器的基础,要想玩得转 NixOS 与 Nix Flakes,享受到它们带来的诸多好处,就必须学会这门语言。
|
||||
|
Loading…
Reference in New Issue
Block a user