A unified Nix configuration supporting both macOS (via nix-darwin) and Linux (via Home Manager standalone).
| Platform | Architecture | Configuration |
|---|---|---|
| macOS | aarch64 (Apple Silicon) | darwinConfigurations."william-2" |
| Linux | x86_64 | homeConfigurations."guoshengwei@linux" |
| Linux | aarch64 | homeConfigurations."guoshengwei@linux-arm" |
/private/etc/nix-darwin/
├── flake.nix # Main entry point
├── flake.lock
├── README.md
│
├── modules/
│ ├── common/ # Cross-platform shared modules
│ │ ├── packages.nix # Cross-platform packages (~70+)
│ │ ├── home-base.nix # Home Manager base settings
│ │ └── programs/
│ │ ├── zsh.nix # Zsh configuration (with platform detection)
│ │ ├── git.nix # Git configuration
│ │ ├── direnv.nix # Direnv configuration
│ │ └── vim.nix # Vim configuration
│ │
│ ├── darwin/ # macOS-specific modules
│ │ ├── default.nix # Darwin system configuration
│ │ ├── homebrew.nix # Homebrew packages
│ │ ├── window-manager.nix # yabai/skhd configuration
│ │ └── home.nix # macOS Home Manager extensions
│ │
│ └── linux/ # Linux-specific modules
│ ├── default.nix # Linux Home Manager entry
│ └── packages.nix # Linux-specific packages (X11, GNOME)
│
├── hosts/ # Host-specific configurations
│ ├── william-2/default.nix # macOS host
│ └── linux/default.nix # Linux host
│
├── users/ # User configurations
│ └── guoshengwei/default.nix
│
└── config/ # Dotfiles
├── doom/ # Doom Emacs configuration
├── gitconfig/ # Git configuration files
├── vimrc # Vim configuration
├── yabairc # yabai configuration (macOS)
├── skhdrc # skhd configuration (macOS)
└── ...
# Build the configuration
darwin-rebuild build --flake .#william-2
# Apply the configuration
darwin-rebuild switch --flake .#william-2# Install Nix (if not already installed)
sh <(curl -L https://nixos.org/nix/install) --daemon
# Install Home Manager
nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager
nix-channel --update
# Apply the configuration (x86_64)
nix run home-manager -- switch --flake .#guoshengwei@linux
# Or for ARM Linux
nix run home-manager -- switch --flake .#guoshengwei@linux-arm| Category | Packages |
|---|---|
| Development | gcc, cmake, nodejs, python313, go, clisp |
| CLI Tools | bat, eza, tree, lazygit, direnv |
| Scientific Computing | eigen, ceres-solver, openblas, hdf5, suitesparse |
| Image Processing | tesseract, ghostscript, libwebp, libjpeg_turbo |
| Font Rendering | fontconfig, freetype, harfbuzz, cairo, pango |
| Compression | openssl, lzo, libb2, libdeflate |
| Category | Packages |
|---|---|
| Window Manager | yabai, skhd |
| Editor | emacs-plus@29 |
| TeX | mactex-no-gui |
| Fonts | font-meslo-lg-nerd-font |
| Category | Packages |
|---|---|
| X11 | xorg.libX11, xorg.libXext, xorg.libxcb, etc. |
| GNOME/GTK | gdk-pixbuf, dbus, at-spi2-core, adwaita-icon-theme |
- Cross-platform package: Add to
modules/common/packages.nix - macOS-only package: Add to
modules/darwin/homebrew.nix(for Homebrew) ormodules/darwin/home.nix(for Nix) - Linux-only package: Add to
modules/linux/packages.nix
Create a new file in modules/common/programs/ and import it in modules/common/home-base.nix:
# modules/common/programs/myprogram.nix
{ config, pkgs, lib, isDarwin ? true, ... }:
{
programs.myprogram = {
enable = true;
# Platform-specific settings
settings = lib.optionalAttrs isDarwin {
# macOS settings
} // lib.optionalAttrs (!isDarwin) {
# Linux settings
};
};
}- Create
hosts/hostname/default.nix - Add the configuration to
flake.nix
The configuration uses an isDarwin boolean passed to modules:
{ config, pkgs, lib, isDarwin ? true, ... }:
{
# Cross-platform settings
home.packages = with pkgs; [
common-package
] ++ lib.optionals isDarwin [
darwin-only-package
] ++ lib.optionals (!isDarwin) [
linux-only-package
];
# Conditional strings in config
programs.zsh.initExtra = ''
# Common config
${lib.optionalString isDarwin ''
# macOS-specific
''}
'';
}# Check flake syntax
nix flake check
# Build with verbose output
darwin-rebuild build --flake .#william-2 --show-trace# Check if Nix is properly configured
nix --version
# Build with verbose output
home-manager switch --flake .#guoshengwei@linux --show-trace- Check if the platform detection is working correctly
- Verify
isDarwinis being passed to the zsh module - Check
~/.zshrcfor any syntax errors
The config/doom/config.el uses platform detection:
(when (eq system-type 'darwin)
(setq mac-option-modifier 'meta)
(setq mac-right-option-modifier 'meta))Enter the development shell:
nix developThis provides:
nixpkgs-fmt- Nix code formatternil- Nix LSP for IDE integration
Personal configuration - feel free to use as a reference for your own setup.