mirror of
https://github.com/ryan4yin/nixos-and-flakes-book.git
synced 2025-02-05 21:20:19 +01:00
feat: nixo with flakes enabled
This commit is contained in:
parent
755961e7a7
commit
b8653668c3
@ -62,11 +62,14 @@ Note that the copied template cannot be used directly. You need to modify it to
|
||||
{
|
||||
description = "Ryan's NixOS Flake";
|
||||
|
||||
# This is the standard format for flake.nix. `inputs` are the dependencies of the flake,
|
||||
# This is the standard format for flake.nix.
|
||||
# `inputs` are the dependencies of the flake,
|
||||
# and `outputs` function will return all the build results of the flake.
|
||||
# Each item in `inputs` will be passed as a parameter to the `outputs` function after being pulled and built.
|
||||
# Each item in `inputs` will be passed as a parameter to
|
||||
# the `outputs` function after being pulled and built.
|
||||
inputs = {
|
||||
# There are many ways to reference flake inputs. The most widely used is github:owner/name/reference,
|
||||
# There are many ways to reference flake inputs.
|
||||
# The most widely used is `github:owner/name/reference`,
|
||||
# which represents the GitHub repository URL + branch/commit-id/tag.
|
||||
|
||||
# Official NixOS package source, using nixos-unstable branch here
|
||||
@ -75,7 +78,8 @@ Note that the copied template cannot be used directly. You need to modify it to
|
||||
home-manager = {
|
||||
url = "github:nix-community/home-manager/release-23.05";
|
||||
# The `follows` keyword in inputs is used for inheritance.
|
||||
# Here, `inputs.nixpkgs` of home-manager is kept consistent with the `inputs.nixpkgs` of the current flake,
|
||||
# Here, `inputs.nixpkgs` of home-manager is kept consistent with
|
||||
# the `inputs.nixpkgs` of the current flake,
|
||||
# to avoid problems caused by different versions of nixpkgs.
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
@ -83,45 +87,65 @@ Note that the copied template cannot be used directly. You need to modify it to
|
||||
|
||||
# `outputs` are all the build result of the flake.
|
||||
# A flake can have many use cases and different types of outputs.
|
||||
# parameters in `outputs` are defined in `inputs` and can be referenced by their names.
|
||||
# However, `self` is an exception, This special parameter points to the `outputs` itself (self-reference)
|
||||
# The `@` syntax here is used to alias the attribute set of the inputs's parameter, making it convenient to use inside the function.
|
||||
# parameters in `outputs` are defined in `inputs` and
|
||||
# can be referenced by their names. However, `self` is an exception,
|
||||
# this special parameter points to the `outputs` itself(self-reference)
|
||||
# The `@` syntax here is used to alias the attribute set of the
|
||||
# inputs's parameter, making it convenient to use inside the function.
|
||||
outputs = { self, nixpkgs, ... }@inputs: {
|
||||
nixosConfigurations = {
|
||||
# By default, NixOS will try to refer the nixosConfiguration with its hostname.
|
||||
# so the system named `nixos-test` will use this configuration.
|
||||
# However, the configuration name can also be specified using `sudo nixos-rebuild switch --flake /path/to/flakes/directory#<name>`.
|
||||
# The `nixpkgs.lib.nixosSystem` function is used to build this configuration, the following attribute set is its parameter.
|
||||
# Run `sudo nixos-rebuild switch --flake .#nixos-test` in the flake's directory to deploy this configuration on any NixOS system
|
||||
# By default, NixOS will try to refer the nixosConfiguration with
|
||||
# its hostname, so the system named `nixos-test` will use this one.
|
||||
# However, the configuration name can also be specified using:
|
||||
# sudo nixos-rebuild switch --flake /path/to/flakes/directory#<name>
|
||||
#
|
||||
# The `nixpkgs.lib.nixosSystem` function is used to build this
|
||||
# configuration, the following attribute set is its parameter.
|
||||
#
|
||||
# Run the following command in the flake's directory to
|
||||
# deploy this configuration on any NixOS system:
|
||||
# sudo nixos-rebuild switch --flake .#nixos-test
|
||||
"nixos-test" = nixpkgs.lib.nixosSystem {
|
||||
system = "x86_64-linux";
|
||||
|
||||
# The Nix module system can modularize configuration, improving the maintainability of configuration.
|
||||
# The Nix module system can modularize configuration,
|
||||
# improving the maintainability of configuration.
|
||||
#
|
||||
# Each parameter in the `modules` is a Nix Module, and there is a partial introduction to it in the nixpkgs manual:
|
||||
# Each parameter in the `modules` is a Nix Module, and
|
||||
# there is a partial introduction to it in the nixpkgs manual:
|
||||
# <https://nixos.org/manual/nixpkgs/unstable/#module-system-introduction>
|
||||
# It is said to be partial because the documentation is not complete, only some simple introductions
|
||||
# (such is the current state of Nix documentation...)
|
||||
# A Nix Module can be an attribute set, or a function that returns an attribute set.
|
||||
# If a Module is a function, this function can only have the following parameters:
|
||||
# It is said to be partial because the documentation is not
|
||||
# complete, only some simple introductions.
|
||||
# such is the current state of Nix documentation...
|
||||
#
|
||||
# lib: the nixpkgs function library, which provides many useful functions for operating Nix expressions
|
||||
# https://nixos.org/manual/nixpkgs/stable/#id-1.4
|
||||
# A Nix Module can be an attribute set, or a function that
|
||||
# returns an attribute set. By default, if a Nix Module is a
|
||||
# function, this function can only have the following parameters:
|
||||
#
|
||||
# lib: the nixpkgs function library, which provides many
|
||||
# useful functions for operating Nix expressions:
|
||||
# https://nixos.org/manual/nixpkgs/stable/#id-1.4
|
||||
# config: all config options of the current flake
|
||||
# options: all options defined in all NixOS Modules in the current flake
|
||||
# options: all options defined in all NixOS Modules
|
||||
# in the current flake
|
||||
# pkgs: a collection of all packages defined in nixpkgs.
|
||||
# you can assume its default value is `nixpkgs.legacyPackages."${system}"` for now.
|
||||
# can be customed by `nixpkgs.pkgs` option
|
||||
# modulesPath: the default path of nixpkgs's builtin modules folder,
|
||||
# you can assume its default value is
|
||||
# `nixpkgs.legacyPackages."${system}"` for now.
|
||||
# can be customed by `nixpkgs.pkgs` option
|
||||
# modulesPath: the default path of nixpkgs's modules folder,
|
||||
# used to import some extra modules from nixpkgs.
|
||||
# this parameter is rarely used, you can ignore it for now.
|
||||
# this parameter is rarely used,
|
||||
# you can ignore it for now.
|
||||
#
|
||||
# Only these parameters can be passed by default.
|
||||
# If you need to pass other parameters, you must use `specialArgs` by uncomment the following line
|
||||
# specialArgs = {...} # pass custom arguments into sub module.
|
||||
# If you need to pass other parameters,
|
||||
# you must use `specialArgs` by uncomment the following line:
|
||||
#
|
||||
# specialArgs = {...} # pass custom arguments into all sub module.
|
||||
modules = [
|
||||
# Import the configuration.nix we used before, so that the old configuration file can still take effect.
|
||||
# Note: /etc/nixos/configuration.nix itself is also a Nix Module, so you can import it directly here
|
||||
# Import the configuration.nix here, so that the
|
||||
# old configuration file can still take effect.
|
||||
# Note: configuration.nix itself is also a Nix Module,
|
||||
./configuration.nix
|
||||
];
|
||||
};
|
||||
|
@ -64,7 +64,9 @@ cat flake.nix
|
||||
# 这是 flake.nix 的标准格式,inputs 是 flake 的依赖,outputs 是 flake 的输出
|
||||
# inputs 中的每一项依赖都会在被拉取、构建后,作为参数传递给 outputs 函数
|
||||
inputs = {
|
||||
# flake inputs 有很多种引用方式,应用最广泛的是 github:owner/name/reference,即 github 仓库地址 + branch/commit-id/tag
|
||||
# flake inputs 有很多种引用方式,应用最广泛的格式是:
|
||||
# github:owner/name/reference
|
||||
# 即 github 仓库地址 + branch/commit-id/tag
|
||||
|
||||
# NixOS 官方软件源,这里使用 nixos-unstable 分支
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
@ -72,39 +74,48 @@ cat flake.nix
|
||||
home-manager = {
|
||||
url = "github:nix-community/home-manager/release-23.05";
|
||||
# `follows` 是 inputs 中的继承语法
|
||||
# 这里使 sops-nix 的 `inputs.nixpkgs` 与当前 flake 的 `inputs.nixpkgs` 保持一致,
|
||||
# 避免依赖的 nixpkgs 版本不一致导致问题
|
||||
# 这里使 sops-nix 的 `inputs.nixpkgs` 与当前 flake 的
|
||||
# `inputs.nixpkgs` 保持一致,避免依赖的 nixpkgs 版本不一致导致问题
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
};
|
||||
|
||||
# outputs 即 flake 的所有输出,其中的 nixosConfigurations 即 NixOS 系统配置
|
||||
# 一个 flake 可以有很多用途,也可以有很多种不同的输出,nixosConfigurations 只是其中一种
|
||||
# flake 有很多用途,也可以有很多不同的 outputs,nixosConfigurations 只是其中一种
|
||||
#
|
||||
# outputs 的参数都是 inputs 中定义的依赖项,可以通过它们的名称来引用。
|
||||
# 不过 self 是个例外,这个特殊参数指向 outputs 自身(自引用),以及 flake 根目录
|
||||
# 这里的 @ 语法将函数的参数 attribute set 取了个别名,方便在内部使用
|
||||
outputs = { self, nixpkgs, ... }@inputs: {
|
||||
# 名为 nixosConfigurations 的 outputs 会在执行 `sudo nixos-rebuild switch` 时被使用
|
||||
# 默认情况下上述命令会使用与主机 hostname 同名的 nixosConfigurations
|
||||
# 名为 nixosConfigurations 的 outputs 会在执行 `sudo nixos-rebuild switch`
|
||||
# 时被使用,默认情况下上述命令会使用与主机 hostname 同名的 nixosConfigurations
|
||||
# 但是也可以通过 `--flake /path/to/flake/direcotry#nixos-test` 来指定
|
||||
# 在 flakes 配置文件夹中执行 `sudo nixos-rebuild switch --flake .#nixos-test` 即可部署此配置
|
||||
# 其中 `.` 表示使用当前文件夹的 Flakes 配置,`#` 后面的内容则是 nixosConfigurations 的名称
|
||||
# 在 flakes 配置文件夹中执行如下命令即可部署此配置:
|
||||
# sudo nixos-rebuild switch --flake .#nixos-test
|
||||
# 其中 --flake 后的参数简要说明如下:
|
||||
# 1. `.` 表示使用当前文件夹的 Flakes 配置,
|
||||
# 2. `#` 后面的内容则是 nixosConfigurations 的名称
|
||||
nixosConfigurations = {
|
||||
# hostname 为 nixos-test 的主机会使用这个配置
|
||||
# 这里使用了 nixpkgs.lib.nixosSystem 函数来构建配置,后面的 attributes set 是它的参数
|
||||
# 在 nixos 系统上使用如下命令即可部署此配置:`nixos-rebuild switch --flake .#nixos-test`
|
||||
# 这里使用了 nixpkgs.lib.nixosSystem 函数来构建配置,
|
||||
# 后面的 attributes set 是它的参数,在 nixos 系统上使用如下命令即可部署此配置:
|
||||
# nixos-rebuild switch --flake .#nixos-test
|
||||
"nixos-test" = nixpkgs.lib.nixosSystem {
|
||||
system = "x86_64-linux";
|
||||
|
||||
# Nix 模块系统可将配置模块化,提升配置的可维护性
|
||||
#
|
||||
# modules 中每个参数,都是一个 Nix Module,nixpkgs manual 中有半份介绍它的文档:
|
||||
# <https://nixos.org/manual/nixpkgs/unstable/#module-system-introduction>
|
||||
# modules 中每个参数,都是一个 Nix Module
|
||||
# nixpkgs manual 中有半份介绍它的文档:
|
||||
# <https://nixos.org/manual/nixpkgs/unstable/#module-system-introduction>
|
||||
# 说半份是因为它的文档不全,只有一些简单的介绍(Nix 文档现状...)
|
||||
# Nix Module 可以是一个 attribute set,也可以是一个返回 attribute set 的函数
|
||||
# 如果是函数,那么它的参数就是当前的 NixOS Module 的参数.
|
||||
# 根据 Nix Wiki 对 Nix modules 的描述,Nix modules 函数的参数可以有这几个:
|
||||
#
|
||||
# Nix Module 可以是一个 attribute set,
|
||||
# 也可以是一个返回 attribute set 的函数,如果是函数,
|
||||
# 那么它的参数就是当前的 NixOS Module 的参数.
|
||||
#
|
||||
# 根据 Nix Wiki 对 Nix modules 的描述,默认情况下,
|
||||
# Nix Module 函数的参数可以有这几个:
|
||||
#
|
||||
# lib: nixpkgs 自带的函数库,提供了许多操作 Nix 表达式的实用函数
|
||||
# 详见 https://nixos.org/manual/nixpkgs/stable/#id-1.4
|
||||
@ -113,14 +124,15 @@ cat flake.nix
|
||||
# pkgs: 一个包含所有 nixpkgs 包的集合
|
||||
# 入门阶段可以认为它的默认值为 `nixpkgs.legacyPackages."${system}"`
|
||||
# 可通过 `nixpkgs.pkgs` 这个 option 来自定义 pkgs 的值
|
||||
# modulesPath: 默认 nixpkgs 的内置 Modules 文件夹路径,常用于从 nixpkgs 中导入一些额外的模块
|
||||
# modulesPath: 默认 nixpkgs 的内置 Modules 文件夹路径,
|
||||
# 常用于从 nixpkgs 中导入一些额外的模块
|
||||
# 这个参数通常都用不到,我只在制作 iso 镜像时用到过
|
||||
#
|
||||
# 默认只能传上面这几个参数,如果需要传其他参数,必须使用 specialArgs,你可以取消注释如下这行来启用该参数
|
||||
# 如果需要传其他非默认参数,就得使用 specialArgs,你可以取消注释如下这行来启用该参数
|
||||
# specialArgs = inputs # 将 inputs 中的参数传入所有子模块
|
||||
modules = [
|
||||
# 导入之前我们使用的 configuration.nix,这样旧的配置文件仍然能生效
|
||||
# 注: /etc/nixos/configuration.nix 本身也是一个 Nix Module,因此可以直接在这里导入
|
||||
# 这里导入之前我们使用的 configuration.nix,这样旧的配置文件仍然能生效
|
||||
# 注: configuration.nix 本身也是一个 Nix Module,因此可以直接在这里导入
|
||||
./configuration.nix
|
||||
];
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user