From 68184abd8fb96c6a9b3e4c4b87ea5930211dee6c Mon Sep 17 00:00:00 2001 From: imbytecat Date: Fri, 10 Apr 2026 21:48:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=B8=BA?= =?UTF-8?q?=E4=B8=89=E8=AE=BE=E5=A4=87=E6=9E=B6=E6=9E=84=EF=BC=8Czsh=20?= =?UTF-8?q?=E8=BF=81=E7=A7=BB=E8=87=B3=20fish?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 darwinConfigurations: mac-mini, macbook-air (aarch64-darwin) - WSL 统一用户名为 imbytecat,主机名改为 awesome-* 系列 - zsh 全面迁移至 fish (abbrs, 内置补全/高亮, zoxide --cmd cd) - 激活 nix-darwin 模块: Homebrew, 系统偏好, Touch ID sudo - 移除 bare/standalone 配置及 catppuccin nixosModule from mkDarwin --- README.md | 169 ++++++++++++++-------------------- flake.nix | 50 +++++----- home/shell/default.nix | 2 +- home/shell/fish.nix | 63 +++++++++++++ home/shell/starship.nix | 2 +- home/shell/tools.nix | 13 +-- home/shell/zsh.nix | 72 --------------- hosts/bare/default.nix | 18 ---- hosts/mac-mini/default.nix | 11 +++ hosts/macbook-air/default.nix | 11 +++ hosts/macbook/default.nix | 11 --- lib/default.nix | 1 - modules/darwin/default.nix | 20 ++-- modules/nixos/default.nix | 4 +- 14 files changed, 196 insertions(+), 251 deletions(-) create mode 100644 home/shell/fish.nix delete mode 100644 home/shell/zsh.nix delete mode 100644 hosts/bare/default.nix create mode 100644 hosts/mac-mini/default.nix create mode 100644 hosts/macbook-air/default.nix delete mode 100644 hosts/macbook/default.nix diff --git a/README.md b/README.md index a0a04f2..3eab5a3 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,37 @@ -# NixOS 声明式系统配置 +# Nix Config -使用 [NixOS](https://nixos.org/) + [Home Manager](https://github.com/nix-community/home-manager) + [Flakes](https://nix.dev/concepts/flakes) 声明式管理系统配置。 +使用 [nix-darwin](https://github.com/nix-darwin/nix-darwin) + [NixOS-WSL](https://github.com/nix-community/NixOS-WSL) + [Home Manager](https://github.com/nix-community/home-manager) + [Flakes](https://nix.dev/concepts/flakes) 声明式管理三台设备的系统配置。 -支持 **WSL** 和**裸机**两种部署方式,共享同一套模块。 +## 设备 + +| 设备 | 平台 | Flake 目标 | 主机名 | +|------|------|-----------|--------| +| Mac Mini | aarch64-darwin | `mac-mini` | awesome-mac-mini | +| MacBook Air | aarch64-darwin | `macbook-air` | awesome-macbook-air | +| Windows PC (WSL) | x86_64-linux | `wsl` | awesome-wsl | ## 快速开始 -### WSL +### macOS (Mac Mini / MacBook Air) + +1. 安装 Nix: + +```bash +curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh +``` + +2. 克隆仓库并构建: + +```bash +git clone ~/.config/nix-config +cd ~/.config/nix-config +# Mac Mini: +darwin-rebuild switch --flake .#mac-mini +# MacBook Air: +darwin-rebuild switch --flake .#macbook-air +``` + +### WSL (Windows PC) 1. 安装 [NixOS-WSL](https://github.com/nix-community/NixOS-WSL/releases): @@ -15,137 +40,81 @@ wsl --import NixOS C:\wsl\nixos nixos-wsl.tar.gz wsl -d NixOS ``` -2. 运行安装脚本: - -```bash -bash <(curl -fsSL https://git.furtherverse.com/imbytecat/nix-config/raw/branch/main/scripts/install.sh) -``` - -3. 重新登录,配置 Git 身份: - -```bash -git config --global user.name "你的名字" -git config --global user.email "你的邮箱" -``` - -### 裸机 - -1. 安装 NixOS 基础系统 -2. 运行安装脚本(传入 `bare` 参数): - -```bash -bash <(curl -fsSL https://git.furtherverse.com/imbytecat/nix-config/raw/branch/main/scripts/install.sh) bare -``` - -3. 生成硬件配置并重新应用: +2. 克隆仓库并构建: ```bash +git clone ~/.config/nix-config cd ~/.config/nix-config -sudo nixos-generate-config --show-hardware-config > hosts/bare/hardware-configuration.nix -# 取消 hosts/bare/default.nix 中 imports 的注释 -sudo nixos-rebuild switch --flake .#bare +sudo nixos-rebuild switch --flake .#wsl ``` ## 仓库结构 ``` ├── flake.nix # 入口:输入源 + 输出配置 -├── flake.lock # 依赖锁定文件(需手动生成) +├── flake.lock # 依赖锁定文件 ├── hosts/ -│ ├── wsl/default.nix # WSL:用户、WSL 设置 -│ └── bare/default.nix # 裸机:引导、网络、硬件 +│ ├── mac-mini/default.nix # Mac Mini 特定配置 +│ ├── macbook-air/default.nix# MacBook Air 特定配置 +│ └── wsl/default.nix # WSL 特定配置 ├── modules/ -│ ├── nixos/ # NixOS 专用模块 +│ ├── darwin/default.nix # macOS 模块(Homebrew、系统偏好等) +│ ├── nixos/ # NixOS 模块 │ │ ├── base.nix # 基础包 │ │ ├── docker.nix # Docker 配置 │ │ ├── locale.nix # 区域 / 语言 -│ │ └── default.nix # 入口 -│ ├── darwin/ # macOS 专用模块(预留) -│ └── shared/ # 共享模块 -│ ├── nix.nix # Nix 设置 -│ └── default.nix # 入口 +│ │ └── default.nix # 入口(用户、shell) +│ └── shared/ # 共享模块(Nix 设置) ├── home/ # Home Manager 配置 -│ ├── default.nix # 入口 +│ ├── default.nix # 入口 + 用户级包 │ ├── theme.nix # Catppuccin 主题 │ ├── dev/ # 开发工具 │ │ ├── neovim.nix │ │ ├── languages.nix # 语言运行时、LSP -│ │ ├── git.nix -│ │ └── default.nix +│ │ └── git.nix │ └── shell/ # Shell 配置 -│ ├── zsh.nix +│ ├── fish.nix # Fish shell │ ├── tmux.nix -│ ├── starship.nix -│ ├── tools.nix # fzf, atuin, zoxide 等 -│ └── default.nix -├── lib/ # 辅助函数 -│ └── default.nix +│ ├── starship.nix # Prompt +│ └── tools.nix # fzf, atuin, zoxide 等 +├── lib/default.nix # 构建辅助函数 ├── overlays/ # 自定义包覆盖 -│ └── default.nix -├── pkgs/ # 自定义包(预留) -│ └── default.nix -└── scripts/ - └── install.sh # 一键安装脚本 +└── pkgs/ # 自定义包 ``` -**配置层级**:`hosts/*`(主机特定) → `modules/*`(共享系统) → `home/*`(用户级) - -## 首次设置(重要) - -### 生成 flake.lock - -首次克隆仓库后,必须生成锁定文件以确保依赖版本一致: - -```bash -cd ~/.config/nix-config -nix flake lock -git add flake.lock -git commit -m "Add flake.lock" -``` - -**为什么需要?** `flake.lock` 锁定所有输入(nixpkgs、home-manager 等)的确切版本,确保不同机器构建结果一致。 +**配置层级**:`hosts/*`(主机特定) → `modules/*`(平台模块) → `home/*`(用户级,跨平台共享) ## 日常使用 ```bash -cd ~/.config/nix-config +# 重建(abbreviation 自动选择 darwin-rebuild 或 nixos-rebuild) +rebuild -# 更新配置 -git pull && sudo nixos-rebuild switch --flake .#wsl +# 更新所有依赖 +update -# 更新所有包版本 -nix flake update && sudo nixos-rebuild switch --flake .#wsl - -# 回滚到上一版本 +# 回滚(NixOS) sudo nixos-rebuild switch --rollback -# 清理旧 generation(释放磁盘) -sudo nix-collect-garbage -d +# 清理旧 generation +sudo nix-collect-garbage -d # NixOS +nix-collect-garbage -d # macOS ``` -## 自定义 +## Shell -### 修改用户名 +使用 **Fish** 作为默认 shell,搭配: -编辑 `hosts/wsl/default.nix`(或 `hosts/bare/default.nix`)顶部: +- **Starship** — 跨平台 prompt +- **Atuin** — shell 历史搜索 +- **Zoxide** — 智能 cd(`cd` = zoxide, `cdi` = 交互选择) +- **FZF** — 模糊搜索(Ctrl-R 历史, Ctrl-T 文件, Alt-C 目录) +- **Direnv** — 自动加载项目环境 +- **Catppuccin Mocha** — 统一主题 -```nix -let - username = "你的用户名"; -``` +### 自定义 -### 添加包 - -编辑 `modules/nixos/base.nix` 或 `home/dev/languages.nix`,在对应 `packages` 列表中添加: - -```nix -environment.systemPackages = with pkgs; [ - your-package # ← 添加 -]; -``` - -> 查包名:`nix search nixpkgs <关键词>` 或查看 [NixOS 包搜索](https://search.nixos.org/packages) - -### 添加 Shell 别名 - -编辑 `home/shell/zsh.nix` 中的 `shellAliases`。 +- 添加 fish abbreviation: 编辑 `home/shell/fish.nix` 中的 `shellAbbrs` +- 添加包: 编辑 `home/default.nix` 或 `home/dev/languages.nix` +- 添加 Homebrew cask: 编辑 `modules/darwin/default.nix` 中的 `homebrew.casks` +- 查包名: `nix search nixpkgs <关键词>` 或 [search.nixos.org](https://search.nixos.org/packages) diff --git a/flake.nix b/flake.nix index c8d8af5..3f4f10a 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "Multi-platform Nix configuration — NixOS / nix-darwin / standalone Home Manager"; + description = "Multi-platform Nix configuration — nix-darwin / NixOS-WSL"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; @@ -35,42 +35,34 @@ mylib = import ./lib { inherit inputs; }; in { - # ── NixOS hosts ──────────────────────────────────── + # ── macOS hosts ───────────────────────────────────── + darwinConfigurations = { + mac-mini = mylib.mkDarwin { + hostname = "awesome-mac-mini"; + system = "aarch64-darwin"; + username = "imbytecat"; + extraModules = [ ./hosts/mac-mini ]; + }; + + macbook-air = mylib.mkDarwin { + hostname = "awesome-macbook-air"; + system = "aarch64-darwin"; + username = "imbytecat"; + extraModules = [ ./hosts/macbook-air ]; + }; + }; + + # ── NixOS hosts (WSL on Windows PC) ───────────────── nixosConfigurations = { wsl = mylib.mkNixos { - hostname = "nixos-wsl"; + hostname = "awesome-wsl"; system = "x86_64-linux"; - username = "dev"; + username = "imbytecat"; extraModules = [ inputs.nixos-wsl.nixosModules.default ./hosts/wsl ]; }; - - bare = mylib.mkNixos { - hostname = "nixos"; - system = "x86_64-linux"; - username = "dev"; - extraModules = [ ./hosts/bare ]; - }; - }; - - # ── macOS hosts (uncomment when ready) ───────────── - # darwinConfigurations = { - # macbook = mylib.mkDarwin { - # hostname = "macbook"; - # system = "aarch64-darwin"; - # username = "imbytecat"; - # extraModules = [ ./hosts/macbook ]; - # }; - # }; - - # ── Standalone Home Manager (non-NixOS / non-Darwin) ─ - homeConfigurations = { - "dev" = mylib.mkHome { - system = "x86_64-linux"; - username = "dev"; - }; }; # ── Packages ──────────────────────────────────────── diff --git a/home/shell/default.nix b/home/shell/default.nix index b4d2905..5760afd 100644 --- a/home/shell/default.nix +++ b/home/shell/default.nix @@ -1,6 +1,6 @@ { imports = [ - ./zsh.nix + ./fish.nix ./starship.nix ./tools.nix ./tmux.nix diff --git a/home/shell/fish.nix b/home/shell/fish.nix new file mode 100644 index 0000000..18e38cd --- /dev/null +++ b/home/shell/fish.nix @@ -0,0 +1,63 @@ +{ pkgs, lib, ... }: + +{ + programs.fish = { + enable = true; + + shellAbbrs = { + # Navigation + ".." = "cd .."; + "..." = "cd ../.."; + + # File listing (eza) + ls = "eza --icons --group-directories-first"; + ll = "eza -la --icons --git --group-directories-first"; + la = "eza -a --icons --group-directories-first"; + lt = "eza --tree --level=2 --icons"; + + # Tools + cat = "bat --paging=never"; + rm = "trash-put"; + lg = "lazygit"; + vi = "nvim"; + + # Network + http = "xh"; + + # Nix + update = "nix flake update --flake ~/.config/nix-config"; + }; + + interactiveShellInit = '' + # No greeting + set -g fish_greeting + + # PATH + fish_add_path $HOME/go/bin $HOME/.bun/bin + + # mise + mise activate fish | source + + # Sudo: double Escape to prepend sudo (like zsh sudo plugin) + bind \e\e 'fish_commandline_prepend sudo' + + # Platform-specific rebuild command + if test (uname) = Darwin + abbr --add rebuild "darwin-rebuild switch --flake ~/.config/nix-config" + else + abbr --add rebuild "sudo nixos-rebuild switch --flake ~/.config/nix-config" + end + + # WSL clipboard + if set -q WSL_DISTRO_NAME + alias pbcopy clip.exe + alias pbpaste "powershell.exe -noprofile -c Get-Clipboard" + end + + # User-local overrides + if test -f ~/.config/fish/local.fish + source ~/.config/fish/local.fish + end + ''; + }; +} diff --git a/home/shell/starship.nix b/home/shell/starship.nix index ceddd88..f1bb016 100644 --- a/home/shell/starship.nix +++ b/home/shell/starship.nix @@ -3,7 +3,7 @@ { programs.starship = { enable = true; - enableZshIntegration = true; + enableFishIntegration = true; settings = { add_newline = false; diff --git a/home/shell/tools.nix b/home/shell/tools.nix index 13fc809..b68c580 100644 --- a/home/shell/tools.nix +++ b/home/shell/tools.nix @@ -4,7 +4,7 @@ # ── FZF ────────────────────────────────────────────── programs.fzf = { enable = true; - enableZshIntegration = true; + enableFishIntegration = true; defaultCommand = "fd --type f --hidden --follow --exclude .git"; defaultOptions = [ "--height=40%" @@ -22,7 +22,7 @@ # ── Atuin (shell history) ──────────────────────────── programs.atuin = { enable = true; - enableZshIntegration = true; + enableFishIntegration = true; settings = { enter_accept = true; filter_mode = "host"; @@ -36,13 +36,14 @@ # ── Zoxide (smart cd) ─────────────────────────────── programs.zoxide = { enable = true; - enableZshIntegration = true; + enableFishIntegration = true; + options = [ "--cmd cd" ]; # cd/cdi instead of z/zi }; # ── Direnv + nix-direnv ───────────────────────────── programs.direnv = { enable = true; - enableZshIntegration = true; + enableFishIntegration = true; nix-direnv.enable = true; config.global = { warn_timeout = "120s"; @@ -61,7 +62,7 @@ # ── Eza (ls replacement) ──────────────────────────── programs.eza = { enable = true; - enableZshIntegration = false; # we use custom aliases in zsh.nix + enableFishIntegration = false; # we use custom abbrs in fish.nix git = true; icons = "auto"; extraOptions = [ @@ -73,7 +74,7 @@ # ── Yazi (file manager) ───────────────────────────── programs.yazi = { enable = true; - enableZshIntegration = true; + enableFishIntegration = true; }; # ── Btop (system monitor) ─────────────────────────── diff --git a/home/shell/zsh.nix b/home/shell/zsh.nix deleted file mode 100644 index c2fef8e..0000000 --- a/home/shell/zsh.nix +++ /dev/null @@ -1,72 +0,0 @@ -{ pkgs, ... }: - -{ - programs.zsh = { - enable = true; - autocd = true; - - oh-my-zsh = { - enable = true; - plugins = [ - "git" # git aliases (gst, gco, gp...) - "sudo" # double ESC → prepend sudo - "extract" # x file.tar.gz → auto extract - "direnv" # direnv hook - ]; - }; - - autosuggestion.enable = true; - syntaxHighlighting.enable = true; - - shellAliases = { - # Navigation - cd = "z"; - cdi = "zi"; - ".." = "cd .."; - "..." = "cd ../.."; - - # File listing (eza) - ls = "eza --icons --group-directories-first"; - ll = "eza -la --icons --git --group-directories-first"; - la = "eza -a --icons --group-directories-first"; - lt = "eza --tree --level=2 --icons"; - - # Tools - cat = "bat --paging=never"; - rm = "trash-put"; - lg = "lazygit"; - vi = "nvim"; - - # Network - http = "xh"; - - # Nix shortcuts - rebuild = "sudo nixos-rebuild switch --flake ~/.config/nix-config"; - update = "nix flake update --flake ~/.config/nix-config"; - }; - - initExtra = '' - # ── Shell options ── - setopt INTERACTIVE_COMMENTS - setopt NO_BEEP - - # ── PATH (manual go/bun global tools) ── - export PATH="$HOME/go/bin:$HOME/.bun/bin:$PATH" - - # ── fzf-tab plugin ── - source ${pkgs.zsh-fzf-tab}/share/fzf-tab/fzf-tab.plugin.zsh - - # ── mise ── - eval "$(mise activate zsh)" - - # ── WSL clipboard ── - if [[ -n "$WSL_DISTRO_NAME" ]]; then - alias pbcopy="clip.exe" - alias pbpaste="powershell.exe -noprofile -c Get-Clipboard" - fi - - # ── User-local overrides ── - [[ -f ~/.zshrc.local ]] && source ~/.zshrc.local - ''; - }; -} diff --git a/hosts/bare/default.nix b/hosts/bare/default.nix deleted file mode 100644 index f890656..0000000 --- a/hosts/bare/default.nix +++ /dev/null @@ -1,18 +0,0 @@ -{ username, ... }: - -{ - # ── Boot ───────────────────────────────────────────── - boot.loader.systemd-boot.enable = true; - boot.loader.efi.canTouchEfiVariables = true; - - # ── Network ────────────────────────────────────────── - networking.networkmanager.enable = true; - users.users.${username}.extraGroups = [ "networkmanager" ]; - - # ── Hardware ───────────────────────────────────────── - # After first install, generate and uncomment: - # sudo nixos-generate-config --show-hardware-config > hosts/bare/hardware-configuration.nix - # imports = [ ./hardware-configuration.nix ]; - - system.stateVersion = "24.11"; -} diff --git a/hosts/mac-mini/default.nix b/hosts/mac-mini/default.nix new file mode 100644 index 0000000..1aaeb8a --- /dev/null +++ b/hosts/mac-mini/default.nix @@ -0,0 +1,11 @@ +{ ... }: + +{ + # ── Mac Mini specific ───────────────────────────────── + # Always plugged in — desktop workstation role + + # Touch ID for sudo + security.pam.services.sudo_local.touchIdAuth = true; + + system.stateVersion = 5; +} diff --git a/hosts/macbook-air/default.nix b/hosts/macbook-air/default.nix new file mode 100644 index 0000000..dec0e3d --- /dev/null +++ b/hosts/macbook-air/default.nix @@ -0,0 +1,11 @@ +{ ... }: + +{ + # ── MacBook Air specific ────────────────────────────── + # Portable — battery-conscious settings + + # Touch ID for sudo + security.pam.services.sudo_local.touchIdAuth = true; + + system.stateVersion = 5; +} diff --git a/hosts/macbook/default.nix b/hosts/macbook/default.nix deleted file mode 100644 index 2adfeab..0000000 --- a/hosts/macbook/default.nix +++ /dev/null @@ -1,11 +0,0 @@ -{ username, ... }: - -{ - # ── macOS host-specific configuration ──────────────── - # Uncomment and configure when macOS machine is available. - - # Touch ID for sudo - # security.pam.services.sudo_local.touchIdAuth = true; - - # system.stateVersion = 5; -} diff --git a/lib/default.nix b/lib/default.nix index 5836fec..5220c42 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -59,7 +59,6 @@ in ../modules/shared ../modules/darwin inputs.home-manager.darwinModules.home-manager - inputs.catppuccin.nixosModules.catppuccin (homeManagerConfig username) { networking.hostName = hostname; } ] diff --git a/modules/darwin/default.nix b/modules/darwin/default.nix index 269bf36..e72c733 100644 --- a/modules/darwin/default.nix +++ b/modules/darwin/default.nix @@ -2,12 +2,12 @@ { # ── Shell ────────────────────────────────────────── - programs.zsh.enable = true; + programs.fish.enable = true; # ── User ─────────────────────────────────────────── users.users.${username} = { home = "/Users/${username}"; - shell = pkgs.zsh; + shell = pkgs.fish; }; # ── Fonts ────────────────────────────────────────── @@ -35,12 +35,12 @@ }; # ── Homebrew (GUI apps not in nixpkgs) ───────────── - # homebrew = { - # enable = true; - # casks = [ - # "raycast" - # "arc" - # ]; - # onActivation.cleanup = "zap"; - # }; + homebrew = { + enable = true; + casks = [ + "raycast" + "arc" + ]; + onActivation.cleanup = "zap"; + }; } diff --git a/modules/nixos/default.nix b/modules/nixos/default.nix index 8504d44..252478d 100644 --- a/modules/nixos/default.nix +++ b/modules/nixos/default.nix @@ -8,12 +8,12 @@ ]; # ── Default shell ────────────────────────────────── - programs.zsh.enable = true; + programs.fish.enable = true; # ── Default user ─────────────────────────────────── users.users.${username} = { isNormalUser = true; - shell = pkgs.zsh; + shell = pkgs.fish; extraGroups = [ "wheel" ]; };