我用这个 Repo 来存放我所有的电脑环境和配置文件,这样我可以在任何一台电脑上快速地初始化我的环境,而不用担心配置文件的丢失或遗忘。
理论上我可以只用 Brew 或者只用 nix 来管理所有的软件和配置,但是两者都有各自的问题:
下面的步骤是写给我自己看的,所以有一些地方可能不太详细并且混乱,仅供参考。
我的原则是:
GUI 应用只使用 Homebrew 安装,用单文件 ~/Brewfile
管理即可,比如:
cask "firefox-developer-edition"
cask "google-chrome"
cask "iterm2"
cask "telegram"
cask "visual-studio-code"
mas "CoffeeTea - Prevent Sleep", id: 6443935401
mas "Immersive Translate", id: 6447957425
mas "iPic", id: 1101244278
mas "Microsoft Word", id: 462054704
mas "S3", id: 6447647340
mas "WeChat", id: 836500024
mas "Xcode", id: 497799835
所有的命令行应用,只使用 Nix 安装,也是只用一个文件 ~/flake.nix
管理就行了,比如:
{
description = "my global env";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
devenv.url = "github:cachix/devenv/latest";
disko.url = "github:nix-community/disko";
disko.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs,devenv,disko }: {
# profile for my arm -darwin machine
packages."aarch64-darwin".default = let
system = "aarch64-darwin";
pkgs = (nixpkgs.legacyPackages.${system}.extend (import ./overlays.nix));
in pkgs.buildEnv {
name = "global-env";
paths = with pkgs; [
nixVersions.nix_2_21
bashInteractive
cachix
devenv.packages."${system}".default
git
fzf
inetutils # telnet
awscli2
stripe-cli
jq
# custom packages
(pkgs.callPackage ./packages/whistle/default.nix {})
(pkgs.callPackage ./packages/web-ext/default.nix {})
];
};
};
}
遗憾的事,初始化的时候目前还做不到一键,但是理论上下面的操作可以用一些脚本来自动化,也许未来会优化这里,目前手动也能接受:
手动设置
install xcode tools
xcode-select --install
然后同意协议:
sudo xcodebuild -license
sh <(curl -L https://nixos.org/nix/install)
一路确认就ok,nix 很详细的描述了它具体都做了啥,可以观察学习一下。 我更推荐使用官方的安装脚本,很多人喜欢用第三方的,他们觉得更干净,默认的配置更友好,但是我觉得好像也没差多少,用原生的,可以顺便学习一下。另外就是我昨天测试的时候,发现第三方安装的nix版本不是最新的,并且在我的osx 14 电脑上有权限错误,但是nix官方的脚本是正常的。
运行完成后重启 terminal 客户端
(用于安装 casks)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
mkdir -p ~/inbox
git clone http://github.com/theowenyoung/home ~/inbox/home
cd ~/inbox/home && brew bundle
在浏览器打开 Github 上存放 keepassxc 加密文件的repo,下载密钥文件 main.kdbx
, 用 keepassxc 打开,找到我保存的 ssh 条目,保存该条目下的所有附件到 ~/.ssh/
, (finder 无法直接选中 ~/.ssh
文件夹,需要cmd+shift+g
手动输入该文件夹,选择后,keepassxc软件就会帮我把我的主ssh 下载到本机电脑,这样就可以恢复我的 ssh 文件,随后用于 github repo下载,加密解密密钥等。
用 nix 安装所有的命令行工具
nix --extra-experimental-features "nix-command flakes" profile install --refresh ~
sudo vi /etc/shells
添加下面的内容到最后一行:
/Users/green/.nix-profile/bin/bash
chsh -s ~/.nix-profile/bin/bash
if ! grep -q "# green-bashrc-start" ~/.bash_profile; then
cat >>~/.bash_profile <<EOF
# green-bashrc-start
if [ -f ~/.config/bash/.bashrc ]; then
source ~/.config/bash/.bashrc
fi
# green-bashrc-end
EOF
fi
source it
. ~/.bash_profile
nix 安装的字体不会自动被安装到系统,需要手动打开 font book
, 选择 file
add fonts to current user
, 选择 ~/nix-profile/share/fonts/truetype/nerdfonts
打开 iterm2:
alfred 工作流配置
修改快捷键为 cmd+space
, 移除默认的 spotlight 快捷键
item to finder: https://github.com/LeEnno/alfred-terminalfinder
vimium 浏览器插件配置
unmapAll
map j scrollDown
map k scrollUp
map d scrollPageDown
map a LinkHints.activateModeToOpenInNewTab
map f LinkHints.activateMode
map J nextTab
map K previousTab
map gg scrollToTop
map G scrollToBottom
map h scrollLeft
map l scrollRight
TCP: 34000-37000 UDP: 34000-37000
./.config/bin/ssnow.sh
sudo apt-get -y update
sudo apt -y install snapd
sudo apt -y install sudo
sudo snap install shadowsocks-rust
# use your own ss://xxxxx
export SERVER_URL=
/snap/bin/shadowsocks-rust.sslocal -b 127.0.0.1:1080 --server-url $SERVER_URL &
/snap/bin/shadowsocks-rust.sslocal --protocol http -b 127.0.0.1:8080 --server-url $SERVER_URL &
export http_proxy=http://127.0.0.1:8080
export https_proxy=http://127.0.0.1:8080
export all_proxy=socks5://127.0.0.1:1080
sh <(curl -L https://nixos.org/nix/install) --daemon --yes
exit
# reconnect to ssh
(可选)
临时代理需要重新开启:
export SERVER_URL=
/snap/bin/shadowsocks-rust.sslocal -b 127.0.0.1:1080 --server-url $SERVER_URL &
/snap/bin/shadowsocks-rust.sslocal --protocol http -b 127.0.0.1:8080 --server-url $SERVER_URL &
export http_proxy=http://127.0.0.1:8080
export https_proxy=http://127.0.0.1:8080
export all_proxy=socks5://127.0.0.1:1080
sudo loginctl enable-linger $USER
nix --extra-experimental-features "nix-command flakes" profile install --refresh "github:theowenyoung/home#proxy"
(可选)如果需要安装root only:
sudo su
如果需要代理:
export http_proxy=http://127.0.0.1:8080
export https_proxy=http://127.0.0.1:8080
export all_proxy=socks5://127.0.0.1:1080
安装 rootonly
nix --extra-experimental-features "nix-command flakes" profile install --refresh "github:theowenyoung/home#rootonly"
// infisical 似乎被墙了...?
在这里获取服务器密钥,根据需要选择过期时间
# Get infisical token
export INFISICAL_TOKEN=
# 写入到该地址,root 和 普通用户 应该都需要一份
touch ~/.infisicalenv && chmod 600 ~/.infisicalenv && echo "INFISICAL_TOKEN=$INFISICAL_TOKEN" > ~/.infisicalenv
只读:
cd "$HOME"
rm -rf .git
git init -b main
git remote add origin https://github.com/theowenyoung/home.git
git fetch origin main
git reset --hard origin/main
git branch --set-upstream-to origin/main main
后期可写:
cd "$HOME"
rm -rf .git
git init -b main
git remote add origin https://github.com/theowenyoung/home.git
git fetch origin main
git reset --hard origin/main
git branch --set-upstream-to origin/main main
git remote set-url --push origin [email protected]:theowenyoung/home.git
git remote -v
if ! grep -q "# green-bashrc-start" ~/.bashrc; then
cat >>~/.bashrc <<EOF
# green-bashrc-start
if [ -f ~/.config/bash/.bashrc ]; then
source ~/.config/bash/.bashrc
fi
# green-bashrc-end
EOF
fi
. ~/.bashrc
./.config/ss/init.sh
./.config/ss/up.sh
Clone secret
git clone [email protected]:theowenyoung/secret.git
./.config/clash/init.sh
everything is ok now.
制作 flake.lock 文件
nix flake lock
nix run github:nix-community/nixos-anywhere -- --flake .#nixos [email protected] --build-on-remote
更新:
ssh to server
nixos-rebuild switch --refresh --flake github:theowenyoung/home#nixos
chsh -s /bin/bash