My Nix Journey - Use Nix on Ubuntu
多平台多系统的版本控制工具(?),且能在不破坏环境变量的情况下临时加载程序
安装 Nix
我选择非官方安装程序安装
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
Nix commands
nix search
搜索包在此WebSite搜索,但在终端中也许会更方便。例如,我要搜索 fish
nix search nixpkgs fish
更精准的搜索(多加几个关键词)
~# nix search nixpkgs gh github copilot
* legacyPackages.x86_64-linux.vscode-extensions.github.copilot (1.200.920)
GitHub Copilot uses OpenAI Codex to suggest code and entire functions in real-time right from your editor
或者使用 -e
参数来排除
~# nix search nixpkgs gh github copilot -e "desktop"
* legacyPackages.x86_64-linux.vscode-extensions.github.copilot (1.200.920)
GitHub Copilot uses OpenAI Codex to suggest code and entire functions in real-time right from your editor
但这个操作真的很慢,在我的小流量机器上得好几分钟,但有了缓存之后再次 search
就快很多
如果是小 RAM 机器,search 之前最好先开个 swap,实测 512m+2G 组合还是会卡死,search 速度与 I/O 强相关
nix run
很方便的运行一个我不想安装的软件(例如测试时)
cowsay
~# nix run nixpkgs#cowsay Nix B!
____________
< Nix B! >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
需要加上参数?用
--
隔开
~# nix run nixpkgs#vim -- --help
nix shell
临时载入一个程序,但不破坏 $PATH
~# nix shell nixpkgs#redis
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
~# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
nix profile
探索中
nix develop
为不同项目设置不同的环境
探索中
nix flake
探索中
Home manager
创建文件
结构如下:
├── nix
│ ├── flake.lock
│ ├── flake.nix
│ └── home-manager
│ ├── apps
│ │ └── micro.nix
│ └── home.nix
简单解释:
-
flake.nix
使用此文件来管理通道的输入(包的安装位置)和配置的输出。 -
home-manager/home.nix
在此文件中存储常见的 home-manager 配置。 -
apps/*.nix
我喜欢将应用程序配置存储到此文件夹中的单个文件中。下文有我的 micro 配置。
{
programs.micro = {
enable = true;
settings = {
colorscheme = "material-tc";
mkparents = true;
softwrap = true;
tabmovement = true;
tabsize = 2;
tabstospaces = true;
autosu = true;
};
};
}
创建 flake.nix
{
description = "My Ubuntu Nix";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
nixpkgs,
home-manager,
...
}: let
# system = "aarch64-linux"; If you are running on ARM powered computer
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
homeConfigurations = {
root = home-manager.lib.homeManagerConfiguration {
inherit pkgs;
modules = [
./home-manager/home.nix
];
};
};
};
}
创建 home.nix
{ config, pkgs, ... }:
{
# 引入其他 Nix 配置模块
# Import other Nix configuration modules
imports = [
./apps/micro.nix
];
# 配置用户信息
# Configure user information
home.username = "root";
home.homeDirectory = "/root";
home.stateVersion = "24.05";
home.packages = with pkgs; [
coreutils # 核心工具 Core utilities
findutils # 查找工具 Find utilities
diffutils # 比较工具 Diff utilities
libtirpc # Transport Independent RPC library
rpcsvc-proto # RPC service definitions
zerofree # Zero free blocks from ext2, ext3 and ext4 file-systems
ripgrep # Mod 搜索工具 Rip Grep tool
gawk # 文本处理工具 Text processing tool
gnused # 流编辑器 Stream editor
bash # Bash Shell
zsh # Z Shell
gnutar # 打包工具 Archive tool
gzip # Compression tool
bzip2 # Compression tool
p7zip # Compression tool
zip # Compression tool
xz # Compression tool
unzip # Compression tool
mtr # 网络诊断工具 Network diagnostic tool
curl # 数据传输工具 Data transfer tool
wget # 下载工具 Download tool
openssh # SSH 客户端 SSH client
rsync # 文件同步工具 File synchronization tool
socat # replacement of openbsd-netcat
nmap # 网络扫描工具 Network scanning tool
libnl # Linux Netlink interface library suite
netplan # Backend-agnostic network configuration in YAML
nettools # 网络工具集(如 ifconfig)Network tools suite (e.g., ifconfig)
iproute2 # 网络管理工具 Network management tools
networkmanager # Network configuration and management tool
openvswitch # A multilayer virtual switch
# bpftop
pastebinit # Command-line pastebin client
byobu # Text-based window manager and terminal multiplexer
git # 分布式版本控制工具 Distributed version control tool
gcc # GNU Compiler Collection
gdb # GNU Debugger
less # 文件查看工具 File viewing tool
autoconf # 自动配置工具 Automatic configuration tool
automake # 自动构建工具 Automatic build tool
gnumake # GNU Make
cmake # Cross-platform build system
vim # Vi IMproved
nano # Nano text editor
neovim # Vim-fork focused on extensibility and usability
htop # 交互式进程查看工具 Interactive process viewer
btop # Resource monitor that shows usage and stats for processor, memory, disks, network and processes
less # 文件查看工具 File viewing tool
neofetch # A command-line system information tool
tree # 树形目录查看工具 Tree directory viewing tool
fish # Friendly interactive shell
# system call monitoring
strace # system call monitoring
ltrace # library call monitoring
lsof # list open files
# system tools
sysstat
lm_sensors # for `sensors` command
ethtool
pciutils # lspci
usbutils # lsusb
gh
fio # 磁盘性能测试工具 Disk performance testing tool
asciinema # Record and share terminal sessions
gmp # GNU Multiple Precision Arithmetic Library
mpfr # Multiple-precision floating-point computations with correct rounding
linbox # Make requires a lot of memory
python312Full # Python 3.12
python312Packages.pip # Python 3.12 pip
python312Packages.ipython # Python 3.12 ipython
shadowsocks-rust
sing-box
hysteria
z3
sage # SageMath is a free open-source mathematics software system licensed under the GPL. It combines the power of many existing open-source packages into a common Python-based interface
fplll # fplll contains implementations of several lattice algorithms. The implementation relies on floating-point orthogonalization, and LLL [LLL82] is central to the code
flatter # (f)ast (lat)tice (r)eduction of integer lattice bases
patchelf
pwntools
gef
pwndbg
pwninit
python312Packages.fpylll
python312Packages.pwndbg
python312Packages.pwntools
];
programs.home-manager.enable = true;
}
安装 home-manager
我的 nix/
目录就在我的当前位置,所以用以下命令安装 home-manager 这个软件包即可
nix run nixpkgs#home-manager -- switch --flake nix/#$USER
启用 home-manager
虽然是 nix run,但 home-manager 自己管理自己,使得已经可以用 home-manager CLI 及定义在 home.nix
文件中的 applications
管理 applications
管理各个 applications
的配置文件,下面是文档:
Appendix A. Home Manager Configuration Options
Updates & garbage collect
Updates
进入到 nix
目录
nix flake update
home-manager switch --flake <flake-file-path>#<USERNAME>
我常用的命令:
home-manager switch --flake /$USER/nix/#$USER
garbage collect
nix store gc
后话
还有个奇怪的问题:配置
fish
为默认 command line shell 后,每次开机后的fish
很多命令都不可用,显示找不到路径。这时先跳到bash
,再由bash
打开fish
就 OK 了,分析是后面的操作fish
会从bash
配置文件读配置文件,造成上手就可用的感觉(也许分析错误,求指出)。
破案,
fish
不可用的原因是fish_config
没写nix
相关的配置。
而打开bash
再启动fish
为什么可用呢,因为这时fish
是bash
的子进程,继承了环境变量。
最终使用 fish 插件 nix-env
解决,用 fisher
(需要自己安装) 安装到系统即可
fisher install lilyball/nix-env.fish