mirror of
https://github.com/ryan4yin/nixos-and-flakes-book
synced 2024-11-26 22:10:26 +00:00
feat: the nix language
This commit is contained in:
parent
da367028d6
commit
dbef30972c
2 changed files with 50 additions and 2 deletions
|
@ -7,7 +7,10 @@ The Nix language is a straightforward functional language. If you have some prog
|
|||
To get started, I recommend reading the following resources for a solid introduction to the Nix language:
|
||||
|
||||
1. [**Nix Language Basics - nix.dev**](https://nix.dev/tutorials/first-steps/nix-language): This tutorial provides a comprehensive overview of the basics of the Nix language.
|
||||
3. [Nix - A One Pager](https://github.com/tazjin/nix-1p): A one page introduction to the Nix language.
|
||||
2. [Nix - A One Pager](https://github.com/tazjin/nix-1p): A one page introduction to the Nix language.
|
||||
1. You can safely skip the section on `callPackage`/`Overriding`/`Overlays` for now. We will cover it in the next chapter.
|
||||
3. [Nix Language](https://nixos.org/manual/nix/stable/language/): The official documentation of the Nix language.
|
||||
1. nix.dev & nix-1p are suitable for starter reading only, and **neither of them fully introduces the full syntax of Nix**. If you encounter a new grammar that you have not come across before, please refer to the official document
|
||||
|
||||
By going through these materials, you will develop a solid foundation in the Nix language, allowing you to effectively utilize NixOS and Flakes.
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@ Nix 是一门比较简单的函数式语言,在已有一定编程基础的情
|
|||
|
||||
先把语法过一遍,有个大概的印象就行,后面需要用到时再根据右侧目录回来复习。
|
||||
|
||||
>注:本人经验有限,如下内容不一定包含了所有 Nix 语言的语法,请以官方文档 [Nix Language](https://nixos.org/manual/nix/stable/language/values) 为准。
|
||||
|
||||
## 基础数据类型一览 {#basic-data-types}
|
||||
|
||||
下面通过一个 attribute set (这类似 json 或者其他语言中的 map/dict)来简要说明所有基础数据类型:
|
||||
|
@ -215,6 +217,49 @@ Nix 会在看到 `<nixpkgs>` 这类三角括号语法时,会在 `NIX_PATH` 环
|
|||
''
|
||||
```
|
||||
|
||||
## 多行字符串的转义 {#multi-line-string-escape}
|
||||
|
||||
在单行字符串中,Nix 的转义语法与许多其他语言相同,`"` `\` `${` 以及其他 `\n` `\t` 等特殊字符,都可直接使用 `\` 进行转义,比如:
|
||||
|
||||
```nix
|
||||
"this is a \"string\" \\" # 结果是: this is a "string" \
|
||||
```
|
||||
|
||||
但在多行字符串中,情况会有点特殊。Nix 规定在多行字符串中需要使用两个单引号 `''` 来转义。
|
||||
|
||||
比如如下表示输出原始字符 `${a}`,而不是字符串插值:
|
||||
|
||||
```nix
|
||||
let
|
||||
a = "1";
|
||||
in
|
||||
''the value of a is:
|
||||
''${a}
|
||||
'' # 结果是 "the value of a is ''${a}"
|
||||
```
|
||||
|
||||
其他 `\n` `\t` 等特殊字符的转义也类似,必须使用两个单引号来转义,如
|
||||
|
||||
```nix
|
||||
''
|
||||
this is a
|
||||
multi-line
|
||||
string
|
||||
''\n
|
||||
''
|
||||
```
|
||||
|
||||
如果我们希望在字符串中使用原始字符 `''`,则需要再为它添加一个 `'`,比如:
|
||||
|
||||
```nix
|
||||
let
|
||||
a = "1";
|
||||
in
|
||||
''the value of a is:
|
||||
'''${a}'''
|
||||
'' # 结果是 "the value of a is ''1''"
|
||||
```
|
||||
|
||||
## 函数 {#nix-function}
|
||||
|
||||
函数的声明语法为:
|
||||
|
|
Loading…
Reference in a new issue