Skip to content

Commit f971ec0

Browse files
committed
feat: redesign the project structure
1 parent ab11958 commit f971ec0

124 files changed

Lines changed: 710 additions & 539 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

flake.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{
22
description = "NixOS configuration of Ryan Yin";
33

4-
# flake 为了确保够纯,它不依赖系统自身的 /etc/nix/nix.conf,而是在 flake.nix 中通过 nixConfig 设置
5-
# 但是为了确保安全性,flake 默认仅允许直接设置少数 nixConfig 参数,其他参数都需要在执行 nix 命令时指定 `--accept-flake-config`,否则会被忽略
6-
# <https://nixos.org/manual/nix/stable/command-ref/conf-file.html>
7-
# 如果有些包国内镜像下载不到,它仍然会走国外,这时候就得靠旁路由来解决了。
8-
# 临时修改默认网关为旁路由: sudo ip route add default via 192.168.5.201
9-
# sudo ip route del default via 192.168.5.201
104
nixConfig = {
115
experimental-features = [ "nix-command" "flakes" ];
126

@@ -31,13 +25,17 @@
3125
];
3226
};
3327

34-
# 这是 flake.nix 的标准格式,inputs 是 flake 的依赖,outputs 是 flake 的输出
35-
# inputs 中的每一项都被拉取、构建后,被作为参数传递给 outputs 函数
28+
29+
# This is the standard format for flake.nix. `inputs` are the dependencies of the flake,
30+
# and `outputs` function will return all the build results of the flake.
31+
# Each item in `inputs` will be passed as a parameter to the `outputs` function after being pulled and built.
3632
inputs = {
37-
# flake inputs 有很多种引用方式,应用最广泛的是 github 的引用方式
33+
# There are many ways to reference flake inputs. The most widely used is github:owner/name/reference,
34+
# which represents the GitHub repository URL + branch/commit-id/tag.
3835

39-
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; # 使用 nixos-unstable 分支 for nix flakes
40-
nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-23.05"; # unstable branch may be broken sometimes, use stable branch when necessary
36+
# Official NixOS package source, using nixos-unstable branch here
37+
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
38+
nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-23.05";
4139

4240
# for macos
4341
nixpkgs-darwin.url = "github:nixos/nixpkgs/nixpkgs-23.05-darwin";
@@ -51,10 +49,14 @@
5149
# e.g. wechat-uos/qqmusic/dingtalk
5250
nur.url = "github:nix-community/NUR";
5351

54-
home-manager.url = "github:nix-community/home-manager";
55-
# follows 是 inputs 中的继承语法
56-
# 这里使 home-manager 的 nixpkgs 这个 inputs 与当前 flake 的 inputs.nixpkgs 保持一致,避免依赖的 nixpkgs 版本不一致导致问题
57-
home-manager.inputs.nixpkgs.follows = "nixpkgs";
52+
# home-manager, used for managing user configuration
53+
home-manager = {
54+
url = "github:nix-community/home-manager";
55+
# The `follows` keyword in inputs is used for inheritance.
56+
# Here, `inputs.nixpkgs` of home-manager is kept consistent with the `inputs.nixpkgs` of the current flake,
57+
# to avoid problems caused by different versions of nixpkgs dependencies.
58+
inputs.nixpkgs.follows = "nixpkgs";
59+
};
5860

5961
# modern window compositor
6062
hyprland.url = "github:hyprwm/Hyprland/v0.25.0";
@@ -77,9 +79,11 @@
7779
nil.url = "github:oxalica/nil/2023-05-09";
7880
};
7981

80-
# outputs 的参数都是 inputs 中定义的依赖项,可以通过它们的名称来引用。
81-
# 不过 self 是个例外,这个特殊参数指向 outputs 自身(自引用),以及 flake 根目录
82-
# 这里的 @ 语法将函数的参数 attribute set 取了个别名,方便在内部使用
82+
# `outputs` are all the build result of the flake.
83+
# A flake can have many use cases and different types of outputs.
84+
# parameters in `outputs` are defined in `inputs` and can be referenced by their names.
85+
# However, `self` is an exception, This special parameter points to the `outputs` itself (self-reference)
86+
# The `@` syntax here is used to alias the attribute set of the inputs's parameter, making it convenient to use inside the function.
8387
outputs = inputs@{
8488
self,
8589
nixpkgs,
@@ -88,66 +92,81 @@
8892
...
8993
}: {
9094
nixosConfigurations = {
95+
# By default, NixOS will try to refer the nixosConfiguration with its hostname.
96+
# so the system named `msi-rtx4090` will use this configuration.
97+
# However, the configuration name can also be specified using `sudo nixos-rebuild switch --flake /path/to/flakes/directory#<name>`.
98+
# The `nixpkgs.lib.nixosSystem` function is used to build this configuration, the following attribute set is its parameter.
99+
# Run `sudo nixos-rebuild switch --flake .#msi-rtx4090` in the flake's directory to deploy this configuration on any NixOS system
91100
msi-rtx4090 = nixpkgs.lib.nixosSystem {
92101
system = "x86_64-linux";
93102

94-
specialArgs = inputs;
103+
# The Nix module system can modularize configurations, improving the maintainability of configurations.
104+
#
105+
# Each parameter in the `modules` is a Nix Module, and there is a partial introduction to it in the nixpkgs manual:
106+
# <https://nixos.org/manual/nixpkgs/unstable/#module-system-introduction>
107+
# It is said to be partial because the documentation is not complete, only some simple introductions
108+
# (such is the current state of Nix documentation...)
109+
# A Nix Module can be an attribute set, or a function that returns an attribute set.
110+
# If a Module is a function, according to the Nix Wiki description, this function can have up to four parameters:
111+
#
112+
# config: The configuration of the entire system
113+
# options: All option declarations refined with all definition and declaration references.
114+
# pkgs: The attribute set extracted from the Nix package collection and enhanced with the nixpkgs.config option.
115+
# modulesPath: The location of the module directory of Nix.
116+
#
117+
# Only these four parameters can be passed by default.
118+
# If you need to pass other parameters, you must use `specialArgs` by uncomment the following line
119+
specialArgs = inputs; # pass all inputs into all sub modules.
95120
modules = [
96121
./hosts/msi-rtx4090
97122

98-
# home-manager 作为 nixos 的一个 module
99-
# 这样在 nixos-rebuild switch 时,home-manager 也会被自动部署,不需要额外执行 home-manager switch 命令
123+
# make home-manager as a module of nixos
124+
# so that home-manager configuration will be deployed automatically when executing `nixos-rebuild switch`
100125
home-manager.nixosModules.home-manager
101126
{
102127
home-manager.useGlobalPkgs = true;
103128
home-manager.useUserPackages = true;
104129

105-
# 使用 home-manager.extraSpecialArgs 自定义传递给 ./home 的参数
130+
# pass all inputs into home manager's all sub modules
106131
home-manager.extraSpecialArgs = inputs;
107-
home-manager.users.ryan = import ./home/home-x11.nix;
132+
home-manager.users.ryan = import ./home/linux/x11.nix;
108133
}
109134
];
110135
};
111136

112-
113137
nixos-test = nixpkgs.lib.nixosSystem {
114138
system = "x86_64-linux";
115139
specialArgs = inputs;
116140
modules = [
117141
./hosts/nixos-test
118142

119-
# home-manager 作为 nixos 的一个 module
120-
# 这样在 nixos-rebuild switch 时,home-manager 也会被自动部署,不需要额外执行 home-manager switch 命令
121143
home-manager.nixosModules.home-manager
122144
{
123145
home-manager.useGlobalPkgs = true;
124146
home-manager.useUserPackages = true;
125147

126-
# 使用 home-manager.extraSpecialArgs 自定义传递给 ./home 的参数
127148
home-manager.extraSpecialArgs = inputs;
128-
home-manager.users.ryan = import ./home/home-wayland.nix;
149+
home-manager.users.ryan = import ./home/linux/wayland.nix;
129150
}
130151
];
131152
};
132153
};
133154

155+
# configurations for MacOS
134156
darwinConfigurations."harmonica" = darwin.lib.darwinSystem {
135157
system = "x86_64-darwin";
136158

137159
specialArgs = inputs;
138160
modules = [
139161
./hosts/harmonica
140162

141-
# home-manager 作为 nixos 的一个 module
142-
# 这样在 nixos-rebuild switch 时,home-manager 也会被自动部署,不需要额外执行 home-manager switch 命令
143163
home-manager.darwinModules.home-manager
144164
{
145165
home-manager.useGlobalPkgs = true;
146166
home-manager.useUserPackages = true;
147-
148-
# 使用 home-manager.extraSpecialArgs 自定义传递给 ./home 的参数
167+
149168
home-manager.extraSpecialArgs = inputs;
150-
home-manager.users.admin = import ./home/home-darwin.nix;
169+
home-manager.users.admin = import ./home/darwin;
151170
}
152171
];
153172
};

home/common/core.nix

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{config, pkgs, ...}: let
2+
d = config.xdg.dataHome;
3+
c = config.xdg.configHome;
4+
cache = config.xdg.cacheHome;
5+
in rec {
6+
home.packages = with pkgs; [
7+
neofetch
8+
nnn # terminal file manager
9+
10+
# archives
11+
zip
12+
xz
13+
unzip
14+
p7zip
15+
16+
# utils
17+
ripgrep # recursively searches directories for a regex pattern
18+
jq # A lightweight and flexible command-line JSON processor
19+
yq-go # yaml processer https://github.com/mikefarah/yq
20+
exa # A modern replacement for ‘ls’
21+
22+
# networking tools
23+
mtr # A network diagnostic tool
24+
iperf3
25+
nmap
26+
socat
27+
ldns # replacement of dig, it provide the command `drill`
28+
aria2 # A lightweight multi-protocol & multi-source command-line download utility
29+
socat # replacement of openbsd-netcat
30+
31+
# misc
32+
file
33+
which
34+
tree
35+
gnused
36+
gnutar
37+
gawk
38+
p7zip
39+
xz
40+
zstd
41+
42+
# productivity
43+
hugo
44+
];
45+
46+
programs = {
47+
# A terminal multiplexer
48+
tmux = {
49+
enable = true;
50+
};
51+
52+
# a cat(1) clone with syntax highlighting and Git integration.
53+
bat = {
54+
enable = true;
55+
config = {
56+
pager = "less -FR";
57+
theme = "Catppuccin-mocha";
58+
};
59+
themes = {
60+
Catppuccin-mocha = builtins.readFile (pkgs.fetchurl {
61+
url = "https://raw.githubusercontent.com/catppuccin/bat/main/Catppuccin-mocha.tmTheme";
62+
hash = "sha256-qMQNJGZImmjrqzy7IiEkY5IhvPAMZpq0W6skLLsng/w=";
63+
});
64+
};
65+
};
66+
67+
# skim provides a single executable: sk.
68+
# Basically anywhere you would want to use grep, try sk instead.
69+
skim = {
70+
enable = true;
71+
enableBashIntegration = true;
72+
};
73+
};
74+
75+
services = {
76+
# syncthing.enable = true;
77+
};
78+
79+
programs.bash = {
80+
enable = true;
81+
enableCompletion = true;
82+
bashrcExtra = ''
83+
export PATH="$PATH:$HOME/bin:$HOME/.local/bin:$HOME/go/bin"
84+
'';
85+
};
86+
87+
# add environment variables
88+
systemd.user.sessionVariables = {
89+
# clean up ~
90+
LESSHISTFILE = cache + "/less/history";
91+
LESSKEY = c + "/less/lesskey";
92+
WINEPREFIX = d + "/wine";
93+
94+
# set this variable make i3 failed to start
95+
# related issue:
96+
# https://github.com/sddm/sddm/issues/871
97+
# XAUTHORITY = "$XDG_RUNTIME_DIR/Xauthority";
98+
99+
# set default applications
100+
BROWSER = "firefox";
101+
TERMINAL = "alacritty";
102+
103+
# enable scrolling in git diff
104+
DELTA_PAGER = "less -R";
105+
106+
MANPAGER = "sh -c 'col -bx | bat -l man -p'";
107+
};
108+
109+
home.sessionVariables = systemd.user.sessionVariables;
110+
111+
home.shellAliases = {
112+
k = "kubectl";
113+
114+
urldecode = "python3 -c 'import sys, urllib.parse as ul; print(ul.unquote_plus(sys.stdin.read()))'";
115+
urlencode = "python3 -c 'import sys, urllib.parse as ul; print(ul.quote_plus(sys.stdin.read()))'";
116+
};
117+
}

home/common/default.nix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{config, pkgs, ...}: let
2+
d = config.xdg.dataHome;
3+
c = config.xdg.configHome;
4+
cache = config.xdg.cacheHome;
5+
in rec {
6+
imports = [
7+
./nushell
8+
9+
./alacritty.nix
10+
./core.nix
11+
./development.nix
12+
./git.nix
13+
./media.nix
14+
./starship.nix
15+
];
16+
17+
}

0 commit comments

Comments
 (0)