# Install Nix via the recommended multi-user installation:sh<(curl-L https://nixos.org/nix/install)--daemon# Single-user installationsh<(curl-L https://nixos.org/nix/install)--no-daemon
Docker
# Start a Docker shell with Nixdockerrun-itnixos/nix# Or start a Docker shell with Nix exposing a workdir directorymkdirworkdirdockerrun-it-v$(pwd)/workdir:/workdirnixos/nix# The workdir example from above can be also used to start hacking on nixpkgsgitclone--depth=1https://github.com/NixOS/nixpkgs.gitdockerrun-it-v$(pwd)/nixpkgs:/nixpkgsnixos/nixdocker> nix-build-Inixpkgs=/nixpkgs-Ahellodocker> find./result# this symlink points to the build package# Start a Docker shell with NixOSdockerrun-it--namenix-flakes-d--rmnixpkgs/nix-flakesdockerexec-itnix-flakesbashbash-5.2#nixrungithub:helix-editor/helix/master
# UEFI(GPT)
# Create a GPT partition table
parted /dev/sda -- mklabel gpt
# Add the boot partition.
parted /dev/sda -- mkpart ESP fat32 1MB 512MB
# Add the root partition.
parted /dev/sda -- mkpart root ext4 512MB 100%
# NixOS by default uses the ESP (EFI system partition) as its /boot partition. It uses the initially reserved 512MiB at the start of the disk.
parted /dev/sda -- set 1 esp on
# Legacy Boot(MBR)
# Create a MBR partition table.
parted /dev/sda -- mklabel msdos
parted /dev/sda -- mkpart primary 1MB -2GB
parted /dev/sda -- set 1 boot on
parted /dev/sda -- mkpart primary linux-swap -2GB 100%
# Format
mkfs.fat -F 32 -n boot /dev/sda1
mkfs.ext4 -L nixos /dev/sda2
## Examples
# For initialising Ext4 partitions: mkfs.ext4. It is recommended that you assign a unique symbolic label to the file system using the option -L label, since this makes the file system configuration independent from device changes. For example:
mkfs.ext4 -L nixos /dev/sda1
# For creating swap partitions: mkswap. Again it’s recommended to assign a label to the swap partition: -L label. For example:
mkswap -L swap /dev/sda2
# UEFI systems
# For creating boot partitions: mkfs.fat. Again it’s recommended to assign a label to the boot partition: -n label. For example:
mkfs.fat -F 32 -n boot /dev/sda3
# For creating LVM volumes, the LVM commands, e.g., pvcreate, vgcreate, and lvcreate.
# For creating software RAID devices, use mdadm.
# Mount the target file system on which NixOS should be installed on /mnt, e.g.
mount /dev/sda2/ /mnt
# UEFI systems
# Mount the boot file system on /mnt/boot
mkdir -p /mnt/boot
mount /dev/sda1/ /mnt/boot
# Generate and edit config
nixos-generate-config --root /mnt
cat >> /mnt/etc/nixos/configuration.nix << "EOF"
{ config, lib, pkgs, ... }:
{
imports =
[ # Include the results of the hardware scan.
./hardware-configuration.nix
];
# (for BIOS systems only)
# boot.loader.grub.device = "/dev/sda";
# (for UEFI systems only)
# Use the systemd-boot EFI boot loader.
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
networking.hostName = "logic-nixos"; # Define your hostname.
# Set your time zone.
time.timeZone = "Asia/Shanghai";
# Define a user account. Don't forget to set a password with ‘passwd’.
users.users.logic = {
isNormalUser = true;
extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
openssh.authorizedKeys.keys = [
# replace with your own public key
"ssh-rsa <public-key> logic@nixos"
];
packages = with pkgs; [
tree
];
};
# Enable experimental-features Flakes and nix-command
nix.settings.experimental-features = [ "nix-command" "flakes" ];
# List packages installed in system profile. To search, run:
# $ nix search wget
environment.systemPackages = with pkgs; [
git
vim
wget
curl
# inputs.helix.packages."${pkgs.system}".helix
];
environment.variables.EDITOR = "vim";
# Enable the OpenSSH daemon.
services.openssh = {
enable = true;
settings = {
X11Forwarding = true;
PermitRootLogin = "no";
PasswordAuthentication = false;
};
openFirewall = true;
}
system.stateVersion = "23.11"; # Did you read the comment?
}
EOF
# Install NixOS
nixos-install
# set root password and reboot
# build new configuration and try to realise the configuration in the running system
nixos-rebuild switch
# to build the configuration and switch the running system to it, but without making it the boot default.(so it will get back to a working configuration after the next reboot).
nixos-rebuild test
# to build the configuration and make it the boot default, but not switch to it now (so it will only take effect after the next reboot).
nixos-rebuild boot
# You can make your configuration show up in a different submenu of the GRUB 2 boot screen by giving it a different profile name
nixos-rebuild switch -p test
# to build the configuration but nothing more. can check syntax
nixos-rebuild build
# rollback
nixos-rebuild switch --rollback
# verbose argument
--show-trace --print-build-logs --verbose
# list
nix-channel list
# add new
nix-channel --add https://channels.nixos.org/channel-name nixos
# show flakes templates
nix flake show templates
# init
nix flake init -t templates#full
cat ./flake.nix
# create flake.nix
cat > /etc/nixos/flake.nix << "EOF"
{
description = "A simple NixOS flake";
inputs = {
# NixOS official software source
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
# helix editor, use the master branch
helix.url = "github:helix-editor/helix/master";
};
outputs = { self, nixpkgs, ... }@inputs: {
nixosConfigurations.logic-nixos = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
# Set all inputs as special arguments for all submodules, allowing submodules to directly reference all dependencies in inputs
{ _module.args = { inherit inputs; };}
];
};
};
}
EOF
# modified configuration.nix
cat /etc/nixos/configuration.nix
{ config, pkgs, inputs, ... }:
{
# ...
environment.systemPackages = with pkgs; [
git
vim
wget
curl
# install helix from inputs
inputs.helix.packages."${pkgs.system}".helix
];
# ...
}
...
# switch to flakes
nixos-rebuild switch
# switch to flaskes from specify directory or remote repo
nixos-rebuild switch --flake /path/flake#your-hostname
nixos-rebuild switch --flake github:owner/repo#your-hostname
# flake update
# update all to flake.lock
nix flake update
# or only update home-manager
nix flake lock --update-input home-manager
#
sudo nixos-rebuild switch --flake .
# nix-channel
# no need
# nix-env
nix profile
# nix-shell
nix develop
nix shell
nix run
# eg: only run helix, do not install to system
nix run github:helix-editor/helix/master
# nix-build
nix build
# nix-collect-garbage
nix storage gc --debug
# Interactive environment
nix repl
# init home.nix
cat > /etc/nixos/home.nix << "EOF"
{ config, pkgs, ... }:
{
# user infomation
home.username = "logic";
home.homeDirectory = "/home/logic";
# Directly symlink config files from the current folder to the specified location under the Home directory
# home.file.".config/i3/wallpaper.jpg".source = ./wallpaper.jpg;
# Recursively symlink files from a folder to the specified location under the Home directory
# home.file.".config/i3/scripts" = {
# source = ./scripts;
# recursive = true; # Recurse into the entire folder
# executable = true; # Add execute permission to all files
# };
# Directly hardcode file contents as text in the nix config file
# home.file.".xxx".text = ''
# xxx
# '';
# Set cursor size and font DPI (for 4K displays)
# xresources.properties = {
# "Xcursor.size" = 16;
# "Xft.dpi" = 172;
# };
# Install common software via home.packages
# These packages will only be available for the current user and will not affect system-level configuration
# It is recommended to install all GUI software and CLI software not closely related to the OS via home.packages
home.packages = with pkgs;[
# archives
zip
xz
unzip
p7zip
# utils
jq # A lightweight and flexible command-line JSON processor
yq-go # yaml processor https://github.com/mikefarah/yq
# networking tools
mtr # A network diagnostic tool
iperf3
dnsutils # `dig` + `nslookup`
ldns # replacement of `dig`, it provide the command `drill`
socat # replacement of openbsd-netcat
nmap # A utility for network discovery and security auditing
ipcalc # it is a calculator for the IPv4/v6 addresses
# misc
cowsay
file
which
tree
gnused
gnutar
gawk
zstd
gnupg
# nix related
#
# it provides the command `nom` works just like `nix`
# with more details log output
nix-output-monitor
# productivity
glow # markdown previewer in terminal
# system call monitoring
strace # system call monitoring
ltrace # library call monitoring
lsof # list open files
# system tools
btop # replacement of htop/nmon
iotop # io monitoring
iftop # network monitoring
sysstat
lm_sensors # for `sensors` command
ethtool
pciutils # lspci
usbutils # lsusb
];
# git configuration
programs.git = {
enable = true;
userName = "Logic";
userEmail = "[email protected]";
};
# Enable starship, a beautiful shell prompt
programs.starship = {
enable = true;
# Custom configuration
settings = {
add_newline = false;
aws.disabled = true;
gcloud.disabled = true;
line_break.disabled = true;
};
};
# alacritty - a cross-platform terminal with GPU acceleration
programs.alacritty = {
enable = true;
# Custom configuration
settings = {
env.TERM = "xterm-256color";
font = {
size = 12;
draw_bold_text_with_bright_colors = true;
};
scrolling.multiplier = 5;
selection.save_to_clipboard = true;
};
};
programs.bash = {
enable = true;
enableCompletion = true;
# Add your custom bashrc content here
bashrcExtra = ''
export PATH="$PATH:$HOME/bin:$HOME/.local/bin:$HOME/go/bin"
'';
# Set aliases for convenience, add or remove based on your needs
shellAliases = {
k = "kubectl";
urldecode = "python3 -c 'import sys, urllib.parse as ul; print(ul.unquote_plus(sys.stdin.read()))'";
urlencode = "python3 -c 'import sys, urllib.parse as ul; print(ul.quote_plus(sys.stdin.read()))'";
};
};
home.stateVersion = "23.11";
programs.home-manager.enable = true;
}
EOF
# generate flake.nix
nix flake new example -t github:nix-community/home-manager#nixos
vim /etc/nixos/flake.nix
{
description = "NixOS configuration";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
home-manager = {
url = "github:nix-community/home-manager/release-23.11";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs@{ nixpkgs, home-manager, ... }: {
nixosConfigurations = {
logic-nixos = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
# Configure home-manager as a NixOS module
home-manager.nixosModules.home-manager
{
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.users.logic = import ./home.nix;
}
];
};
};
};
}
# switch to home-manager
nixos-rebuild switch
# all configuration
# Auto-generated version lock file, records all input data sources, hash values, and version numbers for the entire flake, ensuring system reproducibility
/etc/nixos/flake.lock
# Entry file for the flake, recognized and deployed when running sudo nixos-rebuild switch
/etc/nixos/flake.nix
# Imported as a system module in flake.nix, all system-level configuration is currently written in this file
/etc/nixos/configuration.nix
# Imported by home-manager in flake.nix as the user configuration, contains all Home Manager settings for the user, responsible for managing the Home directory
/etc/nixos/home.nix
# hardware configuration
/etc/nixos/hardware-configuration.nix
# example
tree /etc/nixos
/etc/nixos
├── flake.lock
├── flake.nix
├── home
│ ├── default.nix # Import all submodules here via imports = [...]
│ ├── fcitx5 # fcitx5 Chinese input method settings, using a custom Xiaohe Shuangpin input method
│ │ ├── default.nix
│ │ └── rime-data-flypy
│ ├── i3 # i3wm desktop configuration
│ │ ├── config
│ │ ├── default.nix
│ │ ├── i3blocks.conf
│ │ ├── keybindings
│ │ └── scripts
│ ├── programs
│ │ ├── browsers.nix
│ │ ├── common.nix
│ │ ├── default.nix # Import all nix files under the programs directory via imports = [...]
│ │ ├── git.nix
│ │ ├── media.nix
│ │ ├── vscode.nix
│ │ └── xdg.nix
│ ├── rofi # rofi application launcher configuration, triggered by shortcuts configured in i3wm
│ │ ├── configs
│ │ │ ├── arc_dark_colors.rasi
│ │ │ ├── arc_dark_transparent_colors.rasi
│ │ │ ├── power-profiles.rasi
│ │ │ ├── powermenu.rasi
│ │ │ ├── rofidmenu.rasi
│ │ │ └── rofikeyhint.rasi
│ │ └── default.nix
│ └── shell # shell terminal related configuration
│ ├── common.nix
│ ├── default.nix
│ ├── nushell
│ │ ├── config.nu
│ │ ├── default.nix
│ │ └── env.nu
│ ├── starship.nix
│ └── terminals.nix
├── hosts
│ ├── msi-rtx4090 # PC host configuration
│ │ ├── default.nix # Former configuration.nix, most content has been split out to modules
│ │ └── hardware-configuration.nix # Hardware-related configuration, auto-generated during NixOS installation
│ └── my-nixos # Virtual machine configuration for testing
│ ├── default.nix
│ └── hardware-configuration.nix
├── modules # Common configuration split out from configuration.nix
│ ├── i3.nix
│ └── system.nix
└── wallpaper.jpg # Desktop wallpaper, referenced in the i3wm configuration
# repl lib
nix repl -f '<nixpkgs>'
nix-repl> :e lib.mkDefault
##
lib.mkDefault
lib.mkForce
lib.mkOrder
lib.mkBefore
lib.mkAfter