/fleek-nix-darwin

Fleek + Home Manager + nix-darwin

Primary LanguageNixMIT LicenseMIT

Fleek Configuration

Nix Home Manager configs generated by Fleek with a generous heaping of nix-darwin added by @worldofgeese

What is all this?

Fleek is a tool for generating a declarative specification of the tools, settings, and files you want on your system. Home Manager, in turn, depends on Nix to do its work. Nix is a tool for creating reproducible and pure outputs. The benefits of Nix for you is to define your system portably, meaning you can take it with you to whichever system you go.

nix-darwin extends Nix for macOS specific configuration: you can manage your Homebrew packages, Mac App Store applications, even down to enabling your fingerprint to authenticate administrative terminal actions.

Getting started

First, make sure Nix and Fleek are installed on your system. Documentation is available at the Fleek website. Stop and come back here after initializing your first Fleek configuration with nix run "https://getfleek.dev/latest.tar.gz" -- init.

You'll now generate a high bling Fleek configuration that curates a list of developer tools to make your life as a developer easier.

Run fleek generate --level high. For users of macOS, continue to the next section.

For Windows Subsystem on Linux users, go straight to Usage.

For help with any errors or bugs, refer to Troubleshooting or open an issue!

Configuring nix-darwin for macOS users

Create the file darwin.nix in the generated path at ~/.config/fleek under your machine name folder Fleek created. My machine name folder is M-02877 but it won't be yours. You can copy many of my settings, packages, and applications in my own darwin.nix file as you like. The important thing is you minimally include the following in your darwin.nix file:

{pkgs, ...}: {
  # if you use zsh (the default on new macOS installations),
  # you'll need to enable this so nix-darwin creates a zshrc sourcing needed environment changes
  programs.zsh.enable = true;
}

Now we will configure nix-darwin to support Fleek/Home Manager. Open ~/.config/fleek/flake.nix. Above the homeConfigurations block, insert the following:

    darwinConfigurations."your-hostname" = darwin.lib.darwinSystem {
      system = "aarch64-darwin";
      modules = [
        ./your-hostname/darwin.nix
        home-manager.darwinModules.home-manager
        {
          home-manager.useGlobalPkgs = true;
          home-manager.useUserPackages = true;
          home-manager.users.your-username.imports = [
            ./home.nix
            ./path.nix
            ./shell.nix
            ./user.nix
            ./aliases.nix
            ./programs.nix
            ./your-hostname/dktaohan.nix
            ./your-hostname/custom.nix
          ];
        }
      ];
    };

Replace any instances of your-username and your-hostname. By adding a darwinConfigurations block, we enable nix-darwin and allow it to manage our generated Fleek/Home Manager configuration.

For a taste of what's possible with nix-darwin have a look at this repository's darwin.nix inside the M-02877 directory.

Warning

Homebrew users: ensure "/opt/homebrew/bin" is added to ~/.config/fleek/path.nix Look to my darwin.nix file for reference on installing brew packages. Be warned that my configuration assumes all Homebrew packages are managed by nix-darwin. Any unmanaged packages will be uninstalled unless you change onActivation.cleanup = "zap"; to onActivation.cleanup = "none".

You are at last ready to turn the power on: run nix run nix-darwin -- switch --flake ~/.config/fleek.

Open a new terminal window to test your changes and enjoy the fruits of your labors 🍇

Reference

Usage

macOS

# To update your packages
$ nix flake update --flake ~/.config/fleek
# To update your machine with any changes
$ nix run nix-darwin -- switch --flake ~/.config/fleek

Windows Subsystem for Linux

# For brand new machines, initialize Home Manager
$ `nix run home-manager/master -- init --switch ~/.config/fleek` 
# To update your packages
$ nix flake update --flake ~/.config/fleek
# To update your machine with any changes
$ `nix run home-manager/master -- --switch`.

Quality-of-life improvements

You're all set to run and enjoy Nix and Home Manager. If you'd like to improve your experience, the below one-liner prompts for a GitHub personal access token, uses it for fetch operations so you're not rate-limited, and finally turns off the "warning: Git tree '~/local/share/fleek' is dirty" you're likely to see.

echo -n "Enter your GitHub key (get one at https://github.com/settings/tokens): " && \
read github_key && \
printf "%s\n" "experimental-features = nix-command flakes auto-allocate-uids" \
"access-tokens = github.com=$github_key" \
"fallback = true" \
"warn-dirty = false" \
"auto-optimise-store = true" > nix.conf

Uninstall

Since Nix is largely self-contained, uninstalling and reverting to your old configuration is easy! (Thank you, Aram, for these instructions.)

  1. Run /nix/nix-installer uninstall.
  2. Restore .bashrc.bak to .bashrc.
  3. Restore .profile.bak to .profile.

Troubleshooting

  • If you version your configuration with Git, be sure to commit all files otherwise it is likely to run into an error like the below due to a Nix issue:

    error: getting status of '/nix/store/2pjy2jwi803dkhn7l77zyqcqja1gq38i-source/flake.nix': No such file or directory
  • You may receive the error, bash: /home/USERNAME/.nix-profile/bin/starship: No such file or directory on first run of nix run nix-darwin -- switch --flake ~/.config/fleek or nix run home-manager/master -- init --switch ~/.config/fleek.

    Refer to bug: starship wrong path if use-xdg-base-directories = true is set to resolve.

  • Rarely, you may encounter the error

    error:
           … while fetching the input 'git+file:///Users/dktaohan/.local/share/fleek'
    
           error: getting working directory status: invalid data in index - calculated checksum does not match expected

    Run cd ~/.local/share/fleek; git config --local index.skipHash false; git reset --mixed to correct. There are a variety of reasons for this error to occur, some of which you may find in this Nix user Discourse post.