Last active
November 14, 2025 15:20
-
-
Save yasushisakai/c36e1ab25010b7ea69183fbe5859a415 to your computer and use it in GitHub Desktop.
Starter for using nix for MacOS (nix-darwin)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| ########################################################### | |
| # Starter for using nix for MacOS (nix-darwin) | |
| ########################################################### | |
| # Assuming we have a clean state MacOS | |
| # 1. download this: | |
| # 2. Change SETTINGS section!! | |
| # 3. Install nix | |
| # TODO: This is only supported until Jan 2026... | |
| # curl -fsSL https://install.determinate.systems/nix | sh -s -- install --prefer-upstream-nix | |
| # 4. Install brew (https://brew.sh/) | |
| # 5. Rename and put this file in `~/.config/nix/flake.nix` | |
| # 6. sudo nix run nix-darwin -- switch --flake ~/.config/nix | |
| # After that you can use the commands `config` & `rebuild` | |
| # To change and configure the system | |
| description = "a default flake configuration for mac"; | |
| inputs = { | |
| nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; | |
| nix-darwin.url = "github:nix-darwin/nix-darwin/master"; | |
| nix-darwin.inputs.nixpkgs.follows = "nixpkgs"; | |
| home-manager.url = "github:nix-community/home-manager/master"; | |
| home-manager.inputs.nixpkgs.follows = "nixpkgs"; | |
| }; | |
| outputs = | |
| inputs@{ | |
| self, | |
| nix-darwin, | |
| nixpkgs, | |
| home-manager, | |
| }: | |
| let | |
| ########################################################### | |
| # SETTINGS | |
| ########################################################### | |
| username = "change_user_name"; | |
| hostname = "change_host_name"; # = `scutil --get LocalHostName` | |
| # git... It has to be someone... so putting my name here | |
| fullname = "Yasushi Sakai"; | |
| email = "yasushi.accounts@fastmail.com"; | |
| ########################################################### | |
| configuration = | |
| { | |
| pkgs, | |
| inputs, | |
| config, | |
| ... | |
| }: | |
| { | |
| nix.settings.experimental-features = "nix-command flakes"; | |
| nix.gc = { | |
| automatic = true; | |
| # dates = "weekly"; # if not nix-darwin | |
| interval = { | |
| Weekday = 0; | |
| Hour = 0; | |
| Minute = 0; | |
| }; | |
| options = "--delete-older-than 300d"; | |
| }; | |
| # Set Git commit hash for darwin-version. | |
| system.configurationRevision = self.rev or self.dirtyRev or null; | |
| # Used for backwards compatibility, please read the changelog before changing. | |
| system.stateVersion = 6; # don't change this! | |
| system.primaryUser = username; | |
| system.keyboard = { | |
| enableKeyMapping = true; | |
| remapCapsLockToControl = true; | |
| }; | |
| system.defaults = { | |
| dock = { | |
| show-recents = false; | |
| autohide = true; | |
| mru-spaces = false; | |
| persistent-apps = [ | |
| { app = "/System/Applications/System Settings.app"; } | |
| ]; | |
| }; | |
| }; | |
| nixpkgs.hostPlatform = "aarch64-darwin"; | |
| nixpkgs.config.allowUnfree = true; | |
| ##################### | |
| # GLOBAL packages | |
| ##################### | |
| # install tailscale manually: | |
| # https://pkgs.tailscale.com/stable/#macos | |
| environment.systemPackages = with pkgs; [ | |
| htop | |
| tmux | |
| ]; | |
| homebrew = { | |
| enable = true; | |
| onActivation = { | |
| autoUpdate = true; | |
| cleanup = "zap"; | |
| upgrade = true; | |
| }; | |
| ##################### | |
| # Homebrew packages | |
| ##################### | |
| # taps = []; | |
| brews = [ | |
| # "imagemagick" | |
| ]; | |
| casks = [ | |
| # "ghostty" | |
| ]; | |
| # NOTE: You will need to sign-in. These apps will also not get purged through `onActivation.cleanup` | |
| # masApps = { | |
| # "Xcode" = 497799835; | |
| # }; | |
| }; | |
| users.users.${username}.home = "/Users/${username}"; | |
| home-manager = { | |
| useGlobalPkgs = true; | |
| useUserPackages = true; | |
| users.${username} = | |
| { pkgs, lib, ... }: | |
| { | |
| home.stateVersion = "24.11"; # don't change this! | |
| ##################### | |
| # Home Manger packages | |
| ##################### | |
| home.packages = with pkgs; [ | |
| # go | |
| ]; | |
| programs.direnv = { | |
| enable = true; | |
| enableZshIntegration = true; | |
| nix-direnv.enable = true; | |
| }; | |
| programs.neovim = { | |
| enable = true; | |
| defaultEditor = true; | |
| vimAlias = true; | |
| vimdiffAlias = true; | |
| viAlias = true; | |
| }; | |
| programs.zsh = { | |
| enable = true; | |
| shellAliases = { | |
| rebuild = "sudo darwin-rebuild switch --flake ~/.config/nix"; | |
| update = "nix flake update --flake ~/.config/nix"; | |
| cleanup = "sudo nix-env --delete-generations +5"; | |
| config = "nvim ~/.config/nix/flake.nix"; | |
| config-nvim = "nvim ~/.config/nix/nvim/init.lua"; | |
| }; | |
| history = { | |
| append = true; | |
| expireDuplicatesFirst = true; | |
| findNoDups = true; | |
| ignoreAllDups = true; | |
| ignorePatterns = [ | |
| "cd *" | |
| "rm *" | |
| ]; | |
| saveNoDups = true; | |
| }; | |
| }; | |
| programs.git = { | |
| enable = true; | |
| userName = fullname; | |
| userEmail = email; | |
| extraConfig = { | |
| init.defaultBranch = "main"; | |
| pull.rebase = true; | |
| }; | |
| ignores = [ ".DS_Store" ]; | |
| }; | |
| # NOTE: you will need to back up config.local file too | |
| home.file.".ssh/config".text = '' | |
| Include ~/.ssh/config.local | |
| ''; | |
| # Manage .zprofile with cached brew shellenv | |
| home.file.".zprofile".text = ''''; | |
| }; | |
| }; # home-manager | |
| }; | |
| in | |
| { | |
| # Build darwin flake using: | |
| # $ sudo darwin-rebuild build --flake ~/.config/nix/ | |
| darwinConfigurations.${hostname} = nix-darwin.lib.darwinSystem { | |
| modules = [ | |
| configuration | |
| home-manager.darwinModules.home-manager | |
| ]; | |
| }; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment