From 389e78066046a680426cb6b20694bbf1fd51a0e8 Mon Sep 17 00:00:00 2001 From: Wong Ding Feng Date: Sat, 15 Nov 2025 23:17:12 +0800 Subject: [PATCH] temp add --- .envrc | 4 + .gitignore | 24 +++ INSTALL.md | 296 +++++++++++++++++++++++++++++++++++ Makefile | 70 +++++++++ QUICKSTART.md | 260 ++++++++++++++++++++++++++++++ README.md | 249 +++++++++++++++++++++++++++++ common.nix | 63 ++++++++ darwin/configuration.nix | 89 +++++++++++ flake.nix | 36 ++++- home-manager/common.nix | 59 +++++++ home-manager/darwin.nix | 54 +++++++ home-manager/development.nix | 76 +++++++++ home-manager/home.nix | 75 ++++----- home-manager/linux.nix | 51 ++++++ home-manager/shell.nix | 117 ++++++++++++++ home-manager/terminal.nix | 110 +++++++++++++ modules/darwin/default.nix | 7 + nixos/configuration.nix | 47 +----- 18 files changed, 1597 insertions(+), 90 deletions(-) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 INSTALL.md create mode 100644 Makefile create mode 100644 QUICKSTART.md create mode 100644 README.md create mode 100644 common.nix create mode 100644 darwin/configuration.nix create mode 100644 home-manager/common.nix create mode 100644 home-manager/darwin.nix create mode 100644 home-manager/development.nix create mode 100644 home-manager/linux.nix create mode 100644 home-manager/shell.nix create mode 100644 home-manager/terminal.nix create mode 100644 modules/darwin/default.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..5e01acb --- /dev/null +++ b/.envrc @@ -0,0 +1,4 @@ +# Automatically load nix flake devShell when entering this directory +# Requires direnv to be installed and hooked into your shell +use flake + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7700b81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Nix build results +result +result-* + +# Direnv +.direnv/ +.envrc.cache + +# Editor files +.vscode/ +.idea/ +*.swp +*.swo +*~ +.DS_Store + +# Backup files +*.backup +*.bak + +# Hardware configuration (may contain sensitive info) +# Uncomment if you don't want to commit it: +# nixos/hardware-configuration.nix + diff --git a/INSTALL.md b/INSTALL.md new file mode 100644 index 0000000..2c38b73 --- /dev/null +++ b/INSTALL.md @@ -0,0 +1,296 @@ +# Installation Guide + +## First-Time Setup + +### On macOS + +#### 1. Install Nix (if not already installed) + +```bash +sh <(curl -L https://nixos.org/nix/install) +``` + +#### 2. Enable Flakes + +Create or edit `~/.config/nix/nix.conf`: + +```bash +mkdir -p ~/.config/nix +cat < ~/.config/nix/nix.conf +experimental-features = nix-command flakes +EOF +``` + +Restart your terminal or run: +```bash +. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh +``` + +#### 3. Configure Your System + +Edit the following files: + +**In `flake.nix`:** +- Line 82-97: Replace `your-hostname` with your actual hostname (run `hostname` to find it) +- Line 94: Replace `your-username` with your actual username (run `whoami`) + +**In `darwin/configuration.nix`:** +- Line 33-34: Set your hostname +- Line 45: Set your architecture: + - Apple Silicon (M1/M2/M3): `"aarch64-darwin"` + - Intel: `"x86_64-darwin"` +- Line 48-51: Set your username + +**In `home-manager/home.nix`:** +- Line 30: Set your username + +**In `home-manager/development.nix`** (optional): +- Uncomment lines for your git name and email + +#### 4. Build and Apply + +```bash +# Build the darwin configuration (first time) +nix build .#darwinConfigurations.YOUR-HOSTNAME.system --extra-experimental-features "nix-command flakes" + +# Apply it +./result/sw/bin/darwin-rebuild switch --flake .#YOUR-HOSTNAME +``` + +After the first successful build, you can use: +```bash +darwin-rebuild switch --flake .#YOUR-HOSTNAME +``` + +#### 5. Reload Your Shell + +Close and reopen your terminal, or source your shell config: +```bash +# For zsh: +source ~/.zshrc + +# For fish: +source ~/.config/fish/config.fish + +# For bash: +source ~/.bashrc +``` + +### On NixOS + +#### 1. Clone This Repository + +```bash +# As root or with sudo +cd /etc/nixos +git clone . +# Or copy your files to /etc/nixos +``` + +#### 2. Configure Your System + +Edit the following files: + +**In `flake.nix`:** +- Line 59-75: Replace `your-hostname` with your actual hostname +- Line 72: Replace `your-username` with your actual username + +**In `nixos/configuration.nix`:** +- Line 38: Set your hostname +- Line 43-54: Configure your user account +- Uncomment line 47-51 if you want to set an initial password + +**In `home-manager/home.nix`:** +- Line 30: Set your username + +**In `home-manager/development.nix`** (optional): +- Uncomment lines for your git name and email + +#### 3. Generate Hardware Configuration + +If you haven't already: +```bash +sudo nixos-generate-config --show-hardware-config > nixos/hardware-configuration.nix +``` + +#### 4. Build and Apply + +```bash +sudo nixos-rebuild switch --flake .#YOUR-HOSTNAME +``` + +#### 5. Reboot (optional but recommended) + +```bash +sudo reboot +``` + +## Updating Your Configuration + +### Update Flake Inputs (packages) + +```bash +nix flake update +``` + +Then rebuild: +```bash +# macOS: +darwin-rebuild switch --flake .#YOUR-HOSTNAME + +# NixOS: +sudo nixos-rebuild switch --flake .#YOUR-HOSTNAME +``` + +### Making Configuration Changes + +1. Edit the relevant configuration files +2. Rebuild and switch: + +```bash +# macOS: +darwin-rebuild switch --flake . + +# NixOS: +sudo nixos-rebuild switch --flake . +``` + +### Testing Changes Before Applying + +```bash +# macOS: +darwin-rebuild build --flake . + +# NixOS: +sudo nixos-rebuild build --flake . +``` + +If the build succeeds, you can then apply with `switch`. + +## Common Customizations + +### Add User Packages + +Edit `home-manager/common.nix`, `home-manager/development.nix`, or the platform-specific files: + +```nix +home.packages = with pkgs; [ + firefox + vscode + ripgrep +]; +``` + +### Enable a Program + +Most programs can be enabled via home-manager options: + +```nix +programs.firefox.enable = true; +programs.git = { + enable = true; + userName = "Your Name"; + userEmail = "you@example.com"; +}; +``` + +### Add System Packages + +Edit `common.nix` or the platform-specific configuration: + +```nix +environment.systemPackages = with pkgs; [ + vim + wget + curl +]; +``` + +### Customize Shell + +Edit `home-manager/shell.nix` to configure bash, zsh, fish, or starship prompt. + +### macOS System Defaults + +Edit `darwin/configuration.nix` to customize macOS behavior (Dock, Finder, keyboard, etc.). + +## Troubleshooting + +### Build Fails with "experimental Nix feature 'flakes' is disabled" + +Make sure you've enabled flakes in `~/.config/nix/nix.conf`: +``` +experimental-features = nix-command flakes +``` + +### "error: collision between..." + +This means two packages are trying to install the same files. Check for: +- Duplicate packages in system and home-manager configs +- Conflicting package versions + +### macOS: "Could not find nix-darwin" + +On first install, you need to build with the full path: +```bash +nix build .#darwinConfigurations.YOUR-HOSTNAME.system +./result/sw/bin/darwin-rebuild switch --flake . +``` + +### Home Manager File Conflicts + +If home-manager tries to manage files that already exist, back them up: +```bash +# Create backups of existing files +for file in ~/.bashrc ~/.zshrc ~/.config/fish/config.fish; do + [ -f "$file" ] && mv "$file" "$file.backup" +done +``` + +Or use the backup flag: +```bash +home-manager switch --flake . --backup-extension .backup +``` + +### Can't Find Your Hostname + +```bash +# macOS and Linux: +hostname + +# If it's not what you expect, you can change it: +# macOS (temporarily): +sudo scutil --set HostName your-new-hostname + +# NixOS: Set in configuration.nix +``` + +### Rollback to Previous Generation + +If a new configuration breaks something: + +```bash +# macOS: +darwin-rebuild --rollback + +# NixOS: +sudo nixos-rebuild --rollback +``` + +## Next Steps + +After installation: + +1. **Customize your configuration** - Edit the home-manager modules to suit your workflow +2. **Add your development tools** - Uncomment languages/tools you need in `development.nix` +3. **Configure Git** - Set your name and email in `development.nix` +4. **Set up shell** - Choose your preferred shell and customize in `shell.nix` +5. **Explore options** - Check out [Home Manager options](https://nix-community.github.io/home-manager/options.html) + +## Getting Help + +- [NixOS Discourse](https://discourse.nixos.org/) +- [Nix Discord](https://discord.gg/RbvHtGa) +- [r/NixOS](https://www.reddit.com/r/NixOS/) +- [Nix Documentation](https://nixos.org/learn.html) + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..db427d1 --- /dev/null +++ b/Makefile @@ -0,0 +1,70 @@ +.PHONY: help darwin-build darwin-switch nixos-build nixos-switch home-build home-switch update clean fmt check + +# Default target +help: + @echo "Available targets:" + @echo " make darwin-build - Build darwin configuration (macOS)" + @echo " make darwin-switch - Build and activate darwin configuration" + @echo " make nixos-build - Build NixOS configuration (Linux)" + @echo " make nixos-switch - Build and activate NixOS configuration" + @echo " make home-build - Build home-manager configuration (standalone)" + @echo " make home-switch - Build and activate home-manager configuration" + @echo " make update - Update flake inputs" + @echo " make fmt - Format nix files" + @echo " make check - Check flake" + @echo " make clean - Clean build artifacts and old generations" + @echo "" + @echo "Note: Set HOSTNAME variable to use a specific host, e.g.:" + @echo " make darwin-switch HOSTNAME=macbook" + +# Auto-detect hostname +HOSTNAME ?= $(shell hostname | cut -d. -f1) +USERNAME ?= $(shell whoami) + +# macOS targets +darwin-build: + nix build .#darwinConfigurations.$(HOSTNAME).system + +darwin-switch: + darwin-rebuild switch --flake .#$(HOSTNAME) + +# NixOS targets +nixos-build: + nixos-rebuild build --flake .#$(HOSTNAME) + +nixos-switch: + sudo nixos-rebuild switch --flake .#$(HOSTNAME) + +# Home Manager targets (standalone) +home-build: + home-manager build --flake .#$(USERNAME)@$(HOSTNAME) + +home-switch: + home-manager switch --flake .#$(USERNAME)@$(HOSTNAME) + +# Maintenance +update: + nix flake update + +fmt: + nix fmt + +check: + nix flake check + +clean: + @echo "Cleaning old generations..." + @if [ "$$(uname)" = "Darwin" ]; then \ + nix-collect-garbage -d; \ + darwin-rebuild --list-generations | tail -5; \ + else \ + sudo nix-collect-garbage -d; \ + sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | tail -5; \ + fi + +# Git helpers (optional) +.PHONY: commit-update +commit-update: + git add flake.lock + git commit -m "chore: update flake inputs" + diff --git a/QUICKSTART.md b/QUICKSTART.md new file mode 100644 index 0000000..17e9cda --- /dev/null +++ b/QUICKSTART.md @@ -0,0 +1,260 @@ +# Quick Start Guide + +## TL;DR + +### macOS Setup (3 steps) + +```bash +# 1. Configure your info +export USERNAME="your-username" +export HOSTNAME="your-hostname" + +# Edit these files and replace placeholders: +# - flake.nix (line 82-94) +# - darwin/configuration.nix (lines 33, 45, 48) +# - home-manager/home.nix (line 30) + +# 2. Build (first time only) +nix build .#darwinConfigurations.$HOSTNAME.system --extra-experimental-features "nix-command flakes" +./result/sw/bin/darwin-rebuild switch --flake .#$HOSTNAME + +# 3. Apply (after first time) +darwin-rebuild switch --flake . +``` + +### Linux/NixOS Setup (2 steps) + +```bash +# 1. Configure your info +# Edit flake.nix, nixos/configuration.nix, home-manager/home.nix +# Replace placeholders with your username and hostname + +# 2. Apply +sudo nixos-rebuild switch --flake .#your-hostname +``` + +--- + +## What You Get + +### ✅ Cross-Platform Support +- Same config works on macOS (via nix-darwin) and Linux (NixOS) +- Automatic platform detection +- Shared home-manager configuration + +### ✅ Modular Home Manager +Your user environment is organized into: +- **common.nix** - Basic tools (git, ripgrep, etc.) +- **development.nix** - Dev tools (gh, lazygit, direnv) +- **shell.nix** - Shell config (bash/zsh/fish + starship prompt) +- **terminal.nix** - Terminal apps (tmux, neovim, alacritty) +- **linux.nix** - Linux-specific GUI apps +- **darwin.nix** - macOS-specific integrations + +### ✅ Great Defaults +- Modern CLI tools (ripgrep, fd, bat, eza, zoxide, fzf) +- Starship prompt with git integration +- Syntax highlighting and autosuggestion +- Git aliases and better defaults +- macOS system preferences (dock, finder, keyboard) +- Automatic garbage collection +- Nix flakes enabled + +### ✅ Easy Maintenance +```bash +# Update everything +make update && make darwin-switch # or make nixos-switch + +# Format code +make fmt + +# Clean old generations +make clean +``` + +--- + +## File Structure Quick Reference + +``` +├── flake.nix ← Define all your systems here +├── common.nix ← Shared Nix settings +│ +├── darwin/ ← macOS system config +│ └── configuration.nix +│ +├── nixos/ ← Linux system config +│ └── configuration.nix +│ +└── home-manager/ ← Your user environment (SHARED!) + ├── home.nix ← Main entry point + ├── common.nix ← Base config + ├── development.nix ← Dev tools + ├── shell.nix ← Shell setup + ├── terminal.nix ← Terminal apps + ├── linux.nix ← Linux-only + └── darwin.nix ← macOS-only +``` + +--- + +## Common Tasks + +### Add a Package + +**User package** (recommended): +```nix +# home-manager/common.nix +home.packages = with pkgs; [ + firefox + your-package-here +]; +``` + +**System package**: +```nix +# common.nix or darwin/configuration.nix or nixos/configuration.nix +environment.systemPackages = with pkgs; [ + your-package-here +]; +``` + +### Enable a Program + +```nix +# In any home-manager/*.nix file +programs.firefox.enable = true; +programs.vscode = { + enable = true; + extensions = with pkgs.vscode-extensions; [ + bbenoist.nix + ]; +}; +``` + +### Platform-Specific Packages + +```nix +# In home-manager/common.nix or development.nix +home.packages = with pkgs; [ + # Shared + ripgrep + fd +] ++ lib.optionals stdenv.isDarwin [ + # macOS only +] ++ lib.optionals stdenv.isLinux [ + # Linux only +]; +``` + +### Change Shell + +```nix +# In home-manager/shell.nix, set enable = true for your preferred shell: +programs.fish.enable = true; # Fish (recommended) +programs.zsh.enable = true; # Zsh +programs.bash.enable = true; # Bash +``` + +Then on macOS, set in `darwin/configuration.nix`: +```nix +programs.fish.enable = true; # or zsh/bash +``` + +On NixOS, set in `nixos/configuration.nix`: +```nix +users.users.your-username.shell = pkgs.fish; # or zsh/bash +``` + +### Add SSH Keys + +macOS: +```nix +# darwin/configuration.nix +users.users.your-username = { + openssh.authorizedKeys.keys = [ + "ssh-ed25519 AAAA... your-key" + ]; +}; +``` + +NixOS: +```nix +# nixos/configuration.nix +users.users.your-username = { + openssh.authorizedKeys.keys = [ + "ssh-ed25519 AAAA... your-key" + ]; +}; +``` + +### Multiple Machines + +Add entries in `flake.nix`: + +```nix +darwinConfigurations = { + macbook = darwin.lib.darwinSystem { ... }; + mac-mini = darwin.lib.darwinSystem { ... }; +}; + +nixosConfigurations = { + desktop = nixpkgs.lib.nixosSystem { ... }; + laptop = nixpkgs.lib.nixosSystem { ... }; +}; +``` + +Then build with: +```bash +darwin-rebuild switch --flake .#macbook +# or +sudo nixos-rebuild switch --flake .#desktop +``` + +--- + +## Troubleshooting One-Liners + +```bash +# Check flake is valid +nix flake check + +# Show flake outputs +nix flake show + +# Build without switching (test) +darwin-rebuild build --flake . # macOS +sudo nixos-rebuild build --flake . # Linux + +# Rollback +darwin-rebuild --rollback # macOS +sudo nixos-rebuild --rollback # Linux + +# Force rebuild +darwin-rebuild switch --flake . --recreate-lock-file + +# View current generation +darwin-rebuild --list-generations # macOS +sudo nix-env --list-generations --profile /nix/var/nix/profiles/system # Linux +``` + +--- + +## Next Steps + +1. ⚙️ **Customize `home-manager/development.nix`** - Add your dev tools +2. 🎨 **Tweak `home-manager/shell.nix`** - Customize your prompt and aliases +3. 🖥️ **Adjust `darwin/configuration.nix`** - Set macOS preferences +4. 📦 **Add packages** - Browse [search.nixos.org](https://search.nixos.org) +5. 🔧 **Create modules** - Extract common patterns into `modules/home-manager/` + +--- + +## Help & Resources + +- 📖 [Full README](./README.md) - Complete documentation +- 🚀 [INSTALL.md](./INSTALL.md) - Detailed installation guide +- 🔍 [NixOS Search](https://search.nixos.org) - Find packages and options +- 💬 [NixOS Discourse](https://discourse.nixos.org) - Community forum +- 🎮 [Nix Discord](https://discord.gg/RbvHtGa) - Real-time chat + diff --git a/README.md b/README.md new file mode 100644 index 0000000..8bb7abd --- /dev/null +++ b/README.md @@ -0,0 +1,249 @@ +# Cross-Platform Nix Configuration + +A modular Nix configuration that works on both macOS (via nix-darwin) and Linux (NixOS), with maximum config sharing through home-manager. + +## Structure + +``` +├── flake.nix # Main flake configuration +├── common.nix # Shared system-level config (NixOS & nix-darwin) +│ +├── darwin/ # macOS system configuration +│ └── configuration.nix +│ +├── nixos/ # NixOS system configuration +│ ├── configuration.nix +│ └── hardware-configuration.nix +│ +├── home-manager/ # User environment (shared across platforms) +│ ├── home.nix # Main home-manager config +│ ├── common.nix # Base configuration +│ ├── development.nix # Development tools +│ ├── shell.nix # Shell configuration +│ ├── terminal.nix # Terminal apps & utilities +│ ├── linux.nix # Linux-specific overrides +│ └── darwin.nix # macOS-specific overrides +│ +├── modules/ +│ ├── home-manager/ # Shared home-manager modules +│ ├── nixos/ # NixOS-specific modules +│ └── darwin/ # nix-darwin-specific modules +│ +├── overlays/ # Package overlays +│ └── default.nix +│ +└── pkgs/ # Custom packages + └── default.nix +``` + +## Getting Started + +### Prerequisites + +**On macOS:** +1. Install Nix: `sh <(curl -L https://nixos.org/nix/install)` +2. Enable flakes by adding to `~/.config/nix/nix.conf`: + ``` + experimental-features = nix-command flakes + ``` + +**On NixOS:** +Flakes should be enabled via this configuration. + +### Configuration Steps + +1. **Update your username and hostname:** + - In `flake.nix`: Replace `your-username` and `your-hostname` in the configurations + - In `home-manager/home.nix`: Update the username + - In `darwin/configuration.nix` or `nixos/configuration.nix`: Update hostname and user settings + +2. **Set your architecture** (macOS): + - In `darwin/configuration.nix`, set `nixpkgs.hostPlatform` to either: + - `"aarch64-darwin"` (Apple Silicon) + - `"x86_64-darwin"` (Intel) + +3. **Configure Git identity** (optional): + - In `home-manager/development.nix`, uncomment and set: + ```nix + userName = "Your Name"; + userEmail = "your.email@example.com"; + ``` + +### Building & Activating + +**On macOS (first time):** +```bash +# Build the darwin configuration +nix build .#darwinConfigurations.your-hostname.system + +# Activate it +./result/sw/bin/darwin-rebuild switch --flake .#your-hostname +``` + +**On macOS (subsequent builds):** +```bash +darwin-rebuild switch --flake .#your-hostname +``` + +**On NixOS:** +```bash +sudo nixos-rebuild switch --flake .#your-hostname +``` + +**Standalone home-manager** (if not using system integration): +```bash +home-manager switch --flake .#your-username@your-hostname +``` + +## Key Features + +### Platform-Aware Configuration + +The configuration automatically detects the platform using `pkgs.stdenv.isDarwin` and `pkgs.stdenv.isLinux`: + +- Home directory: Automatically uses `/Users/` on macOS, `/home/` on Linux +- Platform-specific packages and services are conditionally enabled +- Shared configuration in `common.nix` for consistent experience + +### Modular Home-Manager + +Your user environment is split into logical modules: + +- **common.nix**: Basic tools everyone needs (git, ripgrep, etc.) +- **development.nix**: Development tools (languages, IDEs, etc.) +- **shell.nix**: Shell configuration (bash, zsh, fish, starship) +- **terminal.nix**: Terminal applications (tmux, neovim, alacritty) +- **linux.nix**: Linux-specific GUI apps and services +- **darwin.nix**: macOS-specific apps and integrations + +### Shared System Configuration + +`common.nix` is imported by both NixOS and nix-darwin configurations, providing: + +- Unified Nix settings (flakes, garbage collection, etc.) +- Shared package overlays +- Consistent nixpkgs configuration + +### Easy to Extend + +- Add new home-manager modules in `modules/home-manager/` +- Add platform-specific modules in `modules/nixos/` or `modules/darwin/` +- Add custom packages in `pkgs/` +- Add overlays in `overlays/` + +## Customization Tips + +### Adding Packages + +**User packages** (home-manager): +Add to the appropriate module: +- `home-manager/common.nix` for shared packages +- `home-manager/linux.nix` for Linux-only +- `home-manager/darwin.nix` for macOS-only +- `home-manager/development.nix` for dev tools + +**System packages**: +Add to `common.nix` for shared system packages, or to the platform-specific config. + +### Enabling Programs + +Most programs can be enabled via home-manager: + +```nix +programs.firefox.enable = true; +programs.vscode.enable = true; +``` + +Check [home-manager options](https://nix-community.github.io/home-manager/options.html) for available programs. + +### Multiple Machines + +Add more configurations in `flake.nix`: + +```nix +darwinConfigurations = { + macbook = darwin.lib.darwinSystem { ... }; + mac-mini = darwin.lib.darwinSystem { ... }; +}; + +nixosConfigurations = { + desktop = nixpkgs.lib.nixosSystem { ... }; + laptop = nixpkgs.lib.nixosSystem { ... }; +}; +``` + +### macOS System Defaults + +The darwin configuration includes sensible defaults for macOS. Customize in `darwin/configuration.nix`: + +- Dock behavior +- Finder settings +- Keyboard settings +- Global domain defaults + +## Maintenance + +**Update flake inputs:** +```bash +nix flake update +``` + +**Garbage collect old generations:** +```bash +# This happens automatically every 7 days, but you can run manually: +nix-collect-garbage -d + +# On NixOS: +sudo nix-collect-garbage -d +``` + +**View generations:** +```bash +# On macOS: +darwin-rebuild --list-generations + +# On NixOS: +sudo nix-env --list-generations --profile /nix/var/nix/profiles/system +``` + +**Rollback to previous generation:** +```bash +# On macOS: +darwin-rebuild --rollback + +# On NixOS: +sudo nixos-rebuild --rollback +``` + +## Resources + +- [Nix Documentation](https://nixos.org/manual/nix/stable/) +- [NixOS Options Search](https://search.nixos.org/options) +- [Home Manager Options](https://nix-community.github.io/home-manager/options.html) +- [nix-darwin Options](https://daiderd.com/nix-darwin/manual/index.html) +- [Nix Language Basics](https://nixos.org/manual/nix/stable/language/) + +## Troubleshooting + +**"error: experimental Nix feature 'flakes' is disabled"** +- Enable flakes in your nix configuration + +**Build fails on macOS** +- Ensure nix-darwin is properly installed +- Check that your architecture is set correctly in `darwin/configuration.nix` + +**Home manager conflicts** +- If you have existing dotfiles, back them up before applying home-manager +- Use `home-manager switch --backup-extension .backup` to preserve existing files + +**"collision between" errors** +- Usually means packages are providing the same files +- Check for duplicate package installations in system vs home-manager + +## Contributing + +Feel free to customize this configuration to your needs! The modular structure makes it easy to: +- Add new feature modules +- Share modules between machines +- Contribute modules back to the community + diff --git a/common.nix b/common.nix new file mode 100644 index 0000000..cbe6670 --- /dev/null +++ b/common.nix @@ -0,0 +1,63 @@ +# Shared system-level configuration for both NixOS and nix-darwin +# Import this in both nixos/configuration.nix and darwin/configuration.nix +{ + inputs, + lib, + config, + pkgs, + ... +}: { + # Shared Nix configuration + nix = let + flakeInputs = lib.filterAttrs (_: lib.isType "flake") inputs; + in { + settings = { + # Enable flakes and new 'nix' command + experimental-features = "nix-command flakes"; + + # Opinionated: disable global registry + flake-registry = ""; + + # Deduplicate and optimize nix store + auto-optimise-store = true; + + # Trusted users (useful for remote builds) + # trusted-users = [ "root" "@wheel" ]; + + # Workaround for https://github.com/NixOS/nix/issues/9574 + nix-path = config.nix.nixPath; + }; + + # Garbage collection + gc = { + automatic = true; + options = "--delete-older-than 7d"; + }; + + # Opinionated: make flake registry and nix path match flake inputs + registry = lib.mapAttrs (_: flake: {inherit flake;}) flakeInputs; + nixPath = lib.mapAttrsToList (n: _: "${n}=flake:${n}") flakeInputs; + }; + + # Shared nixpkgs configuration + nixpkgs = { + overlays = [ + inputs.self.overlays.additions + inputs.self.overlays.modifications + inputs.self.overlays.unstable-packages + ]; + + config = { + allowUnfree = true; + }; + }; + + # Shared environment packages + environment.systemPackages = with pkgs; [ + vim + git + curl + wget + ]; +} + diff --git a/darwin/configuration.nix b/darwin/configuration.nix new file mode 100644 index 0000000..6ed7887 --- /dev/null +++ b/darwin/configuration.nix @@ -0,0 +1,89 @@ +# This is your nix-darwin configuration for macOS +# Use this to configure your macOS system environment +{ + inputs, + lib, + config, + pkgs, + ... +}: { + # You can import other darwin modules here + imports = [ + # Shared configuration between NixOS and nix-darwin + ../common.nix + + # If you want to use modules your own flake exports (from modules/darwin): + # inputs.self.darwinModules.example + + # You can also split up your configuration and import pieces of it here: + # ./brew.nix + # ./defaults.nix + ]; + + # macOS-specific Nix settings (common.nix has the shared ones) + # Any darwin-specific nix configuration can go here + + # Auto upgrade nix package and the daemon service + services.nix-daemon.enable = true; + + # Necessary for using flakes on this system + nix.package = pkgs.nix; + + # TODO: Set your hostname + networking.hostName = "your-hostname"; + networking.computerName = "your-hostname"; + + # Create /etc/zshrc that loads the nix-darwin environment + programs.zsh.enable = true; + programs.fish.enable = true; + + # Used for backwards compatibility, please read the changelog before changing + # $ darwin-rebuild changelog + system.stateVersion = 4; + + # The platform the configuration will be used on + nixpkgs.hostPlatform = "aarch64-darwin"; # or "x86_64-darwin" + + # TODO: Configure your system-wide user settings (groups, etc) + users.users.your-username = { + name = "your-username"; + home = "/Users/your-username"; + }; + + # System-wide packages + environment.systemPackages = with pkgs; [ + vim + ]; + + # macOS system defaults + system.defaults = { + dock = { + autohide = true; + orientation = "bottom"; + showhidden = true; + mru-spaces = false; + }; + + finder = { + AppleShowAllExtensions = true; + FXEnableExtensionChangeWarning = false; + _FXShowPosixPathInTitle = true; + }; + + NSGlobalDomain = { + AppleShowAllExtensions = true; + InitialKeyRepeat = 14; + KeyRepeat = 1; + }; + }; + + # Keyboard and trackpad settings + system.keyboard = { + enableKeyMapping = true; + remapCapsLockToControl = true; + }; + + # Security + security.pam.enableSudoTouchIdAuth = true; +} + diff --git a/flake.nix b/flake.nix index c35b4c0..5fc5a90 100644 --- a/flake.nix +++ b/flake.nix @@ -22,6 +22,7 @@ self, nixpkgs, home-manager, + darwin, ... } @ inputs: let # Supported systems for your flake packages, shell, etc. @@ -61,17 +62,48 @@ modules = [ # > Our main nixos configuration file < ./nixos/configuration.nix + # Integrate home-manager as a NixOS module + home-manager.nixosModules.home-manager + { + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.extraSpecialArgs = {inherit inputs;}; + # FIXME: Replace with your username + home-manager.users.your-username = import ./home-manager/home.nix; + } + ]; + }; + }; + + # nix-darwin configuration entrypoint + # Available through 'darwin-rebuild switch --flake .#your-hostname' + darwinConfigurations = { + # FIXME replace with your hostname + your-hostname = darwin.lib.darwinSystem { + specialArgs = {inherit inputs;}; + modules = [ + # > Our main darwin configuration file < + ./darwin/configuration.nix + # Integrate home-manager as a darwin module + home-manager.darwinModules.home-manager + { + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.extraSpecialArgs = {inherit inputs;}; + # FIXME: Replace with your username + home-manager.users.your-username = import ./home-manager/home.nix; + } ]; }; }; # Standalone home-manager configuration entrypoint # Available through 'home-manager --flake .#your-username@your-hostname' + # Useful for systems where you don't have root access homeConfigurations = { # FIXME replace with your username@hostname "your-username@your-hostname" = home-manager.lib.homeManagerConfiguration { - # Home-manager requires 'pkgs' instance - pkgs = nixpkgs.legacyPackages.x86_64-linux; # FIXME replace x86_64-linux with your architecure + pkgs = nixpkgs.legacyPackages.x86_64-linux; # FIXME: Set to your architecture extraSpecialArgs = {inherit inputs;}; modules = [ # > Our main home-manager configuration file < diff --git a/home-manager/common.nix b/home-manager/common.nix new file mode 100644 index 0000000..9061dc4 --- /dev/null +++ b/home-manager/common.nix @@ -0,0 +1,59 @@ +# Common configuration shared across all platforms +{ + inputs, + lib, + config, + pkgs, + ... +}: { + nixpkgs = { + # You can add overlays here + overlays = [ + # Add overlays your own flake exports (from overlays and pkgs dir): + inputs.self.overlays.additions + inputs.self.overlays.modifications + inputs.self.overlays.unstable-packages + ]; + # Configure your nixpkgs instance + config = { + # Disable if you don't want unfree packages + allowUnfree = true; + }; + }; + + # Enable home-manager + programs.home-manager.enable = true; + + # Basic packages everyone should have + home.packages = with pkgs; [ + # Archives + zip + unzip + + # Utils + ripgrep # Better grep + fd # Better find + jq # JSON processor + yq-go # YAML processor + + # System tools + htop + tree + wget + curl + ]; + + # Basic git configuration + programs.git = { + enable = true; + extraConfig = { + init.defaultBranch = "main"; + pull.rebase = true; + push.autoSetupRemote = true; + }; + }; + + # https://nixos.wiki/wiki/FAQ/When_do_I_update_stateVersion + home.stateVersion = "24.05"; +} + diff --git a/home-manager/darwin.nix b/home-manager/darwin.nix new file mode 100644 index 0000000..c72330b --- /dev/null +++ b/home-manager/darwin.nix @@ -0,0 +1,54 @@ +# macOS-specific home-manager configuration +{ + inputs, + lib, + config, + pkgs, + ... +}: lib.mkIf pkgs.stdenv.isDarwin { + # macOS-specific packages + home.packages = with pkgs; [ + # macOS CLI tools + # m-cli # Swiss Army Knife for macOS + + # GUI applications (if you want to manage them with nix) + # Note: Many prefer to use homebrew for GUI apps on macOS + ]; + + # macOS-specific configurations + home.sessionVariables = { + # Fix for Nix on macOS + NIX_PATH = "nixpkgs=${inputs.nixpkgs}:darwin=${inputs.darwin}"; + }; + + # Fish shell configuration for macOS + programs.fish = lib.mkIf config.programs.fish.enable { + shellInit = '' + # Add Homebrew to PATH if it exists + if test -d /opt/homebrew/bin + fish_add_path /opt/homebrew/bin + end + ''; + }; + + # Zsh configuration for macOS + programs.zsh = lib.mkIf config.programs.zsh.enable { + initExtra = '' + # Add Homebrew to PATH if it exists + if [[ -d /opt/homebrew/bin ]]; then + eval "$(/opt/homebrew/bin/brew shellenv)" + fi + ''; + }; + + # Bash configuration for macOS + programs.bash = lib.mkIf config.programs.bash.enable { + initExtra = '' + # Add Homebrew to PATH if it exists + if [[ -d /opt/homebrew/bin ]]; then + eval "$(/opt/homebrew/bin/brew shellenv)" + fi + ''; + }; +} + diff --git a/home-manager/development.nix b/home-manager/development.nix new file mode 100644 index 0000000..3a18aaf --- /dev/null +++ b/home-manager/development.nix @@ -0,0 +1,76 @@ +# Development tools and environment +{ + inputs, + lib, + config, + pkgs, + ... +}: { + home.packages = with pkgs; [ + # Version control + git + gh # GitHub CLI + lazygit # Terminal UI for git + + # Languages and runtimes (add what you need) + # python311 + # nodejs_20 + # go + # rustc + # cargo + + # Build tools + gnumake + cmake + + # Text editors / IDEs + # neovim + # vscode + + # Docker and containers (Linux only, macOS uses Docker Desktop) + ] ++ lib.optionals pkgs.stdenv.isLinux [ + # docker + # docker-compose + ]; + + # Git configuration for development + programs.git = { + enable = true; + # TODO: Configure your git identity + # userName = "Your Name"; + # userEmail = "your.email@example.com"; + + extraConfig = { + core.editor = "vim"; + merge.conflictstyle = "diff3"; + diff.algorithm = "histogram"; + }; + + # Git aliases + aliases = { + st = "status"; + co = "checkout"; + br = "branch"; + ci = "commit"; + unstage = "reset HEAD --"; + last = "log -1 HEAD"; + lg = "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"; + }; + }; + + # GitHub CLI + programs.gh = { + enable = true; + settings = { + git_protocol = "ssh"; + prompt = "enabled"; + }; + }; + + # Direnv for per-directory environments + programs.direnv = { + enable = true; + nix-direnv.enable = true; + }; +} + diff --git a/home-manager/home.nix b/home-manager/home.nix index 9030601..6986002 100644 --- a/home-manager/home.nix +++ b/home-manager/home.nix @@ -7,60 +7,41 @@ pkgs, ... }: { - # You can import other home-manager modules here + # Import modular configurations imports = [ + # Base configuration shared across all platforms + ./common.nix + + # Feature modules + ./development.nix + ./shell.nix + ./terminal.nix + + # Platform-specific configurations + ./linux.nix + ./darwin.nix + # If you want to use modules your own flake exports (from modules/home-manager): # inputs.self.homeManagerModules.example - - # Or modules exported from other flakes (such as nix-colors): - # inputs.nix-colors.homeManagerModules.default - - # You can also split up your configuration and import pieces of it here: - # ./nvim.nix ]; - nixpkgs = { - # You can add overlays here - overlays = [ - # Add overlays your own flake exports (from overlays and pkgs dir): - inputs.self.overlays.additions - inputs.self.overlays.modifications - inputs.self.overlays.unstable-packages - - # You can also add overlays exported from other flakes: - # neovim-nightly-overlay.overlays.default - - # Or define it inline, for example: - # (final: prev: { - # hi = final.hello.overrideAttrs (oldAttrs: { - # patches = [ ./change-hello-to-hi.patch ]; - # }); - # }) - ]; - # Configure your nixpkgs instance - config = { - # Disable if you don't want unfree packages - allowUnfree = true; - }; - }; - # TODO: Set your username home = { - username = "your-username"; - homeDirectory = "/home/your-username"; + username = lib.mkDefault "your-username"; + homeDirectory = lib.mkDefault ( + if pkgs.stdenv.isDarwin + then "/Users/${config.home.username}" + else "/home/${config.home.username}" + ); }; - # Add stuff for your user as you see fit: - # programs.neovim.enable = true; - # home.packages = with pkgs; [ steam ]; - - # Enable home-manager and git - programs.home-manager.enable = true; - programs.git.enable = true; - - # Nicely reload system units when changing configs - systemd.user.startServices = "sd-switch"; - - # https://nixos.wiki/wiki/FAQ/When_do_I_update_stateVersion - home.stateVersion = "23.05"; + # Platform-aware session variables + home.sessionVariables = { + EDITOR = "nvim"; + VISUAL = "nvim"; + } // lib.optionalAttrs pkgs.stdenv.isDarwin { + # macOS-specific environment variables + } // lib.optionalAttrs pkgs.stdenv.isLinux { + # Linux-specific environment variables + }; } diff --git a/home-manager/linux.nix b/home-manager/linux.nix new file mode 100644 index 0000000..b4185cf --- /dev/null +++ b/home-manager/linux.nix @@ -0,0 +1,51 @@ +# Linux-specific home-manager configuration +{ + inputs, + lib, + config, + pkgs, + ... +}: lib.mkIf pkgs.stdenv.isLinux { + # Linux-specific packages + home.packages = with pkgs; [ + # GUI applications (if you use a desktop environment) + # firefox + # chromium + + # Linux-specific CLI tools + # xclip + # xsel + ]; + + # Systemd user services (Linux only) + systemd.user.startServices = "sd-switch"; + + # X11/Wayland specific configurations + # xsession.enable = true; + # wayland.windowManager.sway.enable = true; + + # GTK theme configuration + gtk = { + enable = true; + # theme = { + # name = "Adwaita-dark"; + # package = pkgs.gnome.gnome-themes-extra; + # }; + }; + + # Qt theme configuration + qt = { + enable = true; + platformTheme.name = "gtk"; + }; + + # Services + services = { + # Clipboard manager + # clipmenu.enable = true; + + # Notification daemon + # dunst.enable = true; + }; +} + diff --git a/home-manager/shell.nix b/home-manager/shell.nix new file mode 100644 index 0000000..f3290c5 --- /dev/null +++ b/home-manager/shell.nix @@ -0,0 +1,117 @@ +# Shell configuration (bash, zsh, fish, etc.) +{ + inputs, + lib, + config, + pkgs, + ... +}: { + # Bash configuration + programs.bash = { + enable = true; + enableCompletion = true; + + shellAliases = { + ll = "ls -la"; + ".." = "cd .."; + "..." = "cd ../.."; + gs = "git status"; + gd = "git diff"; + }; + }; + + # Zsh configuration + programs.zsh = { + enable = true; + enableCompletion = true; + autosuggestion.enable = true; + syntaxHighlighting.enable = true; + + shellAliases = { + ll = "ls -la"; + ".." = "cd .."; + "..." = "cd ../.."; + gs = "git status"; + gd = "git diff"; + nix-gc = "nix-collect-garbage -d"; + }; + + history = { + size = 10000; + path = "${config.home.homeDirectory}/.zsh_history"; + }; + + initExtra = '' + # Custom prompt or additional configuration + setopt HIST_IGNORE_ALL_DUPS + setopt HIST_FIND_NO_DUPS + setopt HIST_REDUCE_BLANKS + ''; + }; + + # Fish configuration + programs.fish = { + enable = true; + + shellAliases = { + ll = "ls -la"; + ".." = "cd .."; + "..." = "cd ../.."; + gs = "git status"; + gd = "git diff"; + }; + + functions = { + mkcd = "mkdir -p $argv[1]; and cd $argv[1]"; + }; + }; + + # Starship prompt (works with all shells) + programs.starship = { + enable = true; + settings = { + add_newline = true; + character = { + success_symbol = "[➜](bold green)"; + error_symbol = "[➜](bold red)"; + }; + package.disabled = true; + }; + }; + + # Zoxide (better cd) + programs.zoxide = { + enable = true; + enableBashIntegration = true; + enableZshIntegration = true; + enableFishIntegration = true; + }; + + # fzf (fuzzy finder) + programs.fzf = { + enable = true; + enableBashIntegration = true; + enableZshIntegration = true; + enableFishIntegration = true; + }; + + # Eza (modern ls replacement) + programs.eza = { + enable = true; + enableBashIntegration = true; + enableZshIntegration = true; + enableFishIntegration = true; + git = true; + icons = true; + }; + + # Bat (better cat) + programs.bat = { + enable = true; + config = { + theme = "TwoDark"; + pager = "less -FR"; + }; + }; +} + diff --git a/home-manager/terminal.nix b/home-manager/terminal.nix new file mode 100644 index 0000000..3b50c80 --- /dev/null +++ b/home-manager/terminal.nix @@ -0,0 +1,110 @@ +# Terminal applications and utilities +{ + inputs, + lib, + config, + pkgs, + ... +}: { + home.packages = with pkgs; [ + # Modern CLI tools + ripgrep # Better grep + fd # Better find + sd # Better sed + du-dust # Better du + duf # Better df + procs # Better ps + bottom # Better top/htop + + # File managers + # ranger + # nnn + # lf + + # Multiplexers + # tmux + # zellij + ]; + + # Tmux configuration + programs.tmux = { + enable = true; + terminal = "tmux-256color"; + historyLimit = 10000; + baseIndex = 1; + keyMode = "vi"; + mouse = true; + + plugins = with pkgs.tmuxPlugins; [ + sensible + yank + vim-tmux-navigator + ]; + + extraConfig = '' + # Split panes using | and - + bind | split-window -h + bind - split-window -v + unbind '"' + unbind % + + # Reload config + bind r source-file ~/.config/tmux/tmux.conf + + # Easy pane switching + bind -n M-Left select-pane -L + bind -n M-Right select-pane -R + bind -n M-Up select-pane -U + bind -n M-Down select-pane -D + ''; + }; + + # Neovim (optional, can be expanded) + programs.neovim = { + enable = true; + defaultEditor = true; + viAlias = true; + vimAlias = true; + + extraConfig = '' + set number + set relativenumber + set expandtab + set tabstop=2 + set shiftwidth=2 + set smartindent + set clipboard=unnamedplus + ''; + }; + + # Alacritty terminal emulator configuration + programs.alacritty = { + enable = true; + settings = { + window = { + padding = { + x = 10; + y = 10; + }; + decorations = "full"; + opacity = 0.95; + }; + + font = { + size = 13.0; + normal = { + family = "JetBrainsMono Nerd Font"; + style = "Regular"; + }; + }; + + colors = { + primary = { + background = "0x1e1e1e"; + foreground = "0xd4d4d4"; + }; + }; + }; + }; +} + diff --git a/modules/darwin/default.nix b/modules/darwin/default.nix new file mode 100644 index 0000000..6f2af5a --- /dev/null +++ b/modules/darwin/default.nix @@ -0,0 +1,7 @@ +# Add your reusable nix-darwin modules to this directory, on their own file. +# These should be stuff you would like to share with others, not your personal configurations. +{ + # List your module files here + # my-module = import ./my-module.nix; +} + diff --git a/nixos/configuration.nix b/nixos/configuration.nix index 9e361a8..d6b1561 100644 --- a/nixos/configuration.nix +++ b/nixos/configuration.nix @@ -9,6 +9,9 @@ }: { # You can import other NixOS modules here imports = [ + # Shared configuration between NixOS and nix-darwin + ../common.nix + # If you want to use modules your own flake exports (from modules/nixos): # inputs.self.nixosModules.example @@ -23,48 +26,10 @@ ./hardware-configuration.nix ]; - nixpkgs = { - # You can add overlays here - overlays = [ - # Add overlays your own flake exports (from overlays and pkgs dir): - inputs.self.overlays.additions - inputs.self.overlays.modifications - inputs.self.overlays.unstable-packages - - # You can also add overlays exported from other flakes: - # neovim-nightly-overlay.overlays.default - - # Or define it inline, for example: - # (final: prev: { - # hi = final.hello.overrideAttrs (oldAttrs: { - # patches = [ ./change-hello-to-hi.patch ]; - # }); - # }) - ]; - # Configure your nixpkgs instance - config = { - # Disable if you don't want unfree packages - allowUnfree = true; - }; - }; - - nix = let - flakeInputs = lib.filterAttrs (_: lib.isType "flake") inputs; - in { - settings = { - # Enable flakes and new 'nix' command - experimental-features = "nix-command flakes"; - # Opinionated: disable global registry - flake-registry = ""; - # Workaround for https://github.com/NixOS/nix/issues/9574 - nix-path = config.nix.nixPath; - }; + # NixOS-specific Nix settings (common.nix has the shared ones) + nix = { # Opinionated: disable channels channel.enable = false; - - # Opinionated: make flake registry and nix path match flake inputs - registry = lib.mapAttrs (_: flake: {inherit flake;}) flakeInputs; - nixPath = lib.mapAttrsToList (n: _: "${n}=flake:${n}") flakeInputs; }; # FIXME: Add the rest of your current configuration @@ -103,5 +68,5 @@ }; # https://nixos.wiki/wiki/FAQ/When_do_I_update_stateVersion - system.stateVersion = "23.05"; + system.stateVersion = "24.05"; }