Nix

nix

references

I mainly followed ChatGPT for quick reference and

documentation

others

nix config reference

install Nix:the package manager

I use Option 2.

It installs Nix with flakes enabled by default.

Option 1: Install Nix on macOS using the official installer

sh <(curl -L https://nixos.org/nix/install)

Option 2: Install Nix on macOS using the Determinate Nix Installer

curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | \
  sh -s -- install

After the installation, open another terminal and try this hello world. Have patient, it might takes a while.

nix run "nixpkgs#hello"
# Hello, world!

install flakes

Already installed with the Determinate Nix Installer.

install nix-darwin

create flake.nix

mkdir -p ~/.config/nix-darwin
cd ~/.config/nix-darwin
nix flake init -t nix-darwin
sed -i '' "s/simple/$(scutil --get LocalHostName)/" flake.nix

Open the created flake.nix, Change nixpkgs.hostPlatform to aarch64-darwin because I’m using Apple Silicon.

install nix-darwin

nix run nix-darwin -- switch --flake ~/.config/nix-darwin

using nix-darwin

Open a new terminal(or like me, open a new tab in iTerm2) and try this.

darwin-rebuild switch --flake ~/.config/nix-darwin

This will be the command you’ll see go through the rest of your life, treasure the moment you haven’t remember it yet.

play with nix

remove vim and install neovim

find the package name in nixpkgs

Add the following Nix code to your NixOS Configuration, usually located in /etc/nixos/configuration.nix

  environment.systemPackages = [
    pkgs.neovim
  ];
# ~/.config/nix-darwin/flake.nix
...
environment.systemPackages = [
    # pkgs.vim # remove this 
    pkgs.neovim
];
  • https://github.com/sum-rock/SumAstroNvim
    • Sum Rock’s AstroNvim Implementation This is an implementation of the AstroNvim configuration/plugin distribution for Neovim using Nix flakes.
    • maybe try it later
    • it is so difficult to use astronvim with nix, the former is download and it is working, the latter is configure everything.
# ~/.config/nix-darwin/flake.nix
# configuration = { pkgs, ... }: {
# add config after pkgs,
    configuration = { pkgs, config, ... }: {
# add pkgs.mkalias to systemPackages
    environment.systemPackages =
    [
        pkgs.mkalias
# add this too
# https://gist.github.com/elliottminns/211ef645ebd484eb9a5228570bb60ec3?permalink_comment_id=5271771#gistcomment-5271771
system.activationScripts.applications.text = let
  env = pkgs.buildEnv {
    name = "system-applications";
    paths = config.environment.systemPackages;
    pathsToLink = "/Applications";
  };
in
  pkgs.lib.mkForce ''
  # Set up applications.
  echo "setting up /Applications..." >&2
  rm -rf /Applications/Nix\ Apps
  mkdir -p /Applications/Nix\ Apps
  find ${env}/Applications -maxdepth 1 -type l -exec readlink '{}' + |
  while read -r src; do
    app_name=$(basename "$src")
    echo "copying $src" >&2
    ${pkgs.mkalias}/bin/mkalias "$src" "/Applications/Nix Apps/$app_name"
  done
      '';

install home-manager with flake

to make the vscode nix format work

install pkgs.nixfmt-rfc-style

add this to vscode config

    "nix.formatterPath": "/run/current-system/sw/bin/nixfmt"

set default shell to fish

To use iterm2 set command to fish might lost the PATH environment. Now use this workaround.

# in home-manager configuration
              programs.fish.enable = true;
in nix-darwin flake.nix
          programs.zsh = {
            enable = true;
            interactiveShellInit = ''
              if [[ $(${pkgs.procps}/bin/ps --no-header --pid=$PPID --format=comm) != "fish" && -z ''${BASH_EXECUTION_STRING} ]]
              then
                shopt -q login_shell && LOGIN_OPTION='--login' || LOGIN_OPTION=""
                exec ${pkgs.fish}/bin/fish $LOGIN_OPTION
              fi
            '';
          };
Do not shoot this.