Luca's nix configuration
1 介绍#
1.1 本仓库#
该仓库包含了我的系统配置。主要目标是拥有一个便携且可复制的系统,以便快速和自动化地引导新系统。理想情况下,可以在不同操作系统之间共享软件包和配置。目前我只使用macOS
系统,但我已经为将来切换到nixOS
做好了准备。
工具:
nix
作为软件包管理器home-manager
用于配置软件包和dotfilesnix-darwin
用于配置macOS系统(例如,它有一个很棒的brew
模块)
1.2 本文件#
这个文件(readme.org
)用于生成(正确的术语是tangle
)我的flake.nix
文件,其中我指定了系统配置的输入和输出。生成的flake.nix
文件导入其他.nix
文件(例如darwin/default.nix
)。
最好以HTML格式查看此文件此处
,或者通过org-mode
(克隆该仓库并在emacs
中打开)。您也可以访问我的博客网站
。
2 flake.nix#
2.1 文件结构#
# 注意:此文件是从readme.org中提取的
# 请勿手动编辑
{
description = "Luca Cambiaghi的darwin配置";
<<inputs>>
outputs = inputs@{ self, nixpkgs, darwin, home-manager, flake-utils, ... }:
let
<<nixpkgs-config>>
<<home-manager-config>>
<<nix-darwin-config>>
in {
darwinConfigurations = {
<<luca-macbookpro>>
<<github-ci>>
};
<<cloud-vm>>
<<home-manager-modules>>
} //
<<dev-shell>>
}
2.2 输入#
inputs = {
# 软件包集
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
nixpkgs-master.url = "github:nixos/nixpkgs/master";
nixpkgs-stable-darwin.url = "github:nixos/nixpkgs/nixpkgs-20.09-darwin";
nixos-stable.url = "github:nixos/nixpkgs/nixos-20.09";
# 环境/系统管理
darwin.url = "github:hardselius/nix-darwin";
darwin.inputs.nixpkgs.follows = "nixpkgs";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
# 其他来源
emacs.url = "github:nix-community/emacs-overlay";
fish-done = { url = "github:franciscolourenco/done"; flake = false; };
flake-compat = { url = "github:edolstra/flake-compat"; flake = false; };
flake-utils.url = "github:numtide/flake-utils";
};
2.3 nixpkgs配置#
overlays
变量中的技巧允许我们使用有问题软件包的稳定版本。您可以在home/default.nix
中使用stable.pkg
而不是pkg
。
nixpkgsConfig = with inputs; {
config = {
allowUnfree = true;
allowUnsupportedSystem = true;
};
overlays = [
(
final: prev:
let
system = prev.stdenv.system;
nixpkgs-stable = if system == "x86_64-darwin" then nixpkgs-stable-darwin else nixos-stable;
in {
master = nixpkgs-master.legacyPackages.${system};
stable = nixpkgs-stable.legacyPackages.${system};
}
)
emacs.overlay
];
};
2.4 home-manager配置#
homeManagerCommonConfig = with self.homeManagerModules; {
imports = [
./home
# configs.git.aliases
# configs.starship.symbols
# programs.kitty.extras
];
};
2.5 nix-darwin配置#
nixDarwinCommonModules = { user }: [
# 主`nix-darwin`配置
./darwin
# `home-manager`模块
home-manager.darwinModules.home-manager
{
nixpkgs = nixpkgsConfig;
# 支持使用`<nixpkgs>`等的旧工作流的hack
# nix.nixPath = { nixpkgs = "$HOME/.config/nixpkgs/nixpkgs.nix"; };
# `home-manager`配置
users.users.${user}.home = "/Users/${user}";
home-manager.useGlobalPkgs = true;
home-manager.users.${user} = homeManagerCommonConfig;
}
];
2.6 luca-macbook-pro#
luca-macbookpro = darwin.lib.darwinSystem {
modules = nixDarwinCommonModules { user = "luca"; } ++ [
{
networking = {
knownNetworkServices = ["Wi-Fi" "Bluetooth PAN" "Thunderbolt Bridge"];
hostName = "luca-macbookpro";
computerName = "luca-macbookpro";
localHostName = "luca-macbookpro";
};
}
];
specialArgs = { inherit inputs nixpkgs; };
};
2.7 github-ci#
githubCI = darwin.lib.darwinSystem {
modules = nixDarwinCommonModules { user = "runner"; } ++ [
({ lib, ... }: { homebrew.enable = lib.mkForce false; })
];
};
2.8 home-manager-modules#
# homeManagerModules = {
# configs.git.aliases = import ./home/configs/git-aliases.nix;
# configs.gh.aliases = import ./home/configs/gh-aliases.nix;
# configs.starship.symbols = import ./home/configs/starship-symbols.nix;
# programs.neovim.extras = import ./home/modules/programs/neovim/extras.nix;
# programs.kitty.extras = import ./home/modules/programs/kitty/extras.nix;
# };
2.9 cloud-vm#
使用nix build .#cloudVM.activationPackage; ./result/activate
进行构建和激活
cloudVM = home-manager.lib.homeManagerConfiguration {
system = "x86_64-linux";
homeDirectory = "/home/luca";
username = "luca";
configuration = {
imports = [ homeManagerCommonConfig ];
nixpkgs = nixpkgsConfig;
};
};
2.10 dev-shell#
inputs.flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in {
devShell = import ./shell.nix { inherit pkgs; };
});
3 实用命令#
3.1 安装nix(flakes)#
感谢https://github.com/kclejeune/system
# 1.
if [[ $(uname -s) == 'Darwin' ]]; then
# sh <(curl -L <https://nixos.org/nix/install>) --daemon --darwin-use-unencrypted-nix-store-volume
sh <(curl -L <https://github.com/numtide/nix-flakes-installer/releases/download/nix-2.4pre20210126_f15f0b8/install>) --daemon --darwin-use-unencrypted-nix-store-volume
else
sh <(curl -L <https://nixos.org/nix/install>) --daemon
fi
# 2.
git clone [email protected]:lccambiaghi/nixpkgs.git ~/git/nixpkgs
# 3.
cd ~/git/nixpkgs && nix build ".#darwinConfigurations.luca-macbookpro.system" && ./result/sw/bin/darwin-rebuild switch --flake .#luca-macbookpro
3.2 darwin-rebuild#
darwin-rebuild build --flake .#luca-macbookpro
# nix build ".#darwinConfigurations.luca-macbookpro.system"
darwin-rebuild switch --flake .#luca-macbookpro
# ./result/sw/bin/darwin-rebuild switch --flake .#luca-macbookpro
3.3 nix flake update#
nix flake update --update-input nixpkgs
4 Nix#
4.1 基础知识#
集合:
{ "a b" = "c"; count = 2; }
列表:
[42 "a b" (3 + 6) [2 3 4] {x = 2;}]
Lambda:
(x: x + x) 21
# 42
let hi = {name, place}: "Hi ${name} in ${place}!";
in hi { name = "Michael"; place = "Austria"; }
带有默认值的参数:
{ pkgs ? import <nixpkgs> {} }:
<nixpkgs>
指的是NIXPATH环境变量中声明的nixpkgs属性的值。
4.2 配置#
配置接受一个属性作为参数,并返回一个属性集
{ pkgs }:
{
packageOverrides = pkgs: {
emacs = pkgs.emacs.override {
withGTK2 = false;
withGTK3 = false;
withXwidgets = false;
};
};
allowUnfree = true;
}
4.3 衍生#
衍生接受输入并产生输出。衍生是惰性的,因此只有当它是其他衍生的输入时才会被评估。
derivation {
name = "hello-world";
system = "x86_64-linux";
outputs = [ "out" ]; # 这是默认值,可以省略。
builder = "${pkgs.bash}/bin/bash";
args = [ "-c" "echo 'Hello world!' > $out" ];
}
衍生的输出存储在nix存储中。每个衍生的输出由一个哈希定义,该哈希编码了所有输入衍生。如果输入中有任何细微变化,输出哈希将发生变化。
4.4 字符串中的转义#
- 您可以使用
'
来转义双引号 - 您可以使用
''
来转义美元符号