Here are some key concepts and components of the Nix ecosystem:
- Nix: a purely functional package manager that provides a declarative approach to managing software dependencies and configurations.
- Nixpkgs: the default package repository for Nix, which contains thousands of pre-built packages for various programming languages, libraries, and tools.
- Flakes: a new feature in Nix that allows you to manage Nix configurations as code, with versioned and reproducible dependencies.
- Home Manager: a tool built on top of Nix that allows you to manage your personal configurations, including shell settings, editor configurations, and other user-specific settings.
The first step is to install Nix on your MacBook. You can do this by running the following command in your terminal:
sh <(curl -L https://nixos.org/nix/install) --darwin-use-unencrypted-nix-store-volume
This will download and install the latest version of Nix, and create a new unencrypted Nix store volume on your Mac. Note that the --darwin-use-unencrypted-nix-store-volume
flag is optional, but recommended for development and testing purposes.
Once the installation is complete, you should have access to the nix command in your terminal.
Next, you'll create a new Nix Flake to manage your configurations. Flakes are managed as code, using a flake.nix
file and a flake.lock
file to define dependencies and versions.
Create a new directory for your Flake, and create a flake.nix
file inside it:
mkdir my-nix-config
cd my-nix-config
touch flake.nix
In your flake.nix
file, you'll define your Flake inputs, including Nixpkgs and Home Manager. Here's an example:
{
description = "My Nix Configurations";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
nixpkgs.inputs.flake-compat.url = "github:numtide/flake-compat";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, home-manager }:
let
systems = [ "x86_64-linux" "aarch64-darwin" "x86_64-darwin" ];
in {
packages = builtins.listToAttrs (builtins.map (
let
pkgs = import nixpkgs {
inherit system;
overlays = [ home-manager.overlay ];
};
homeConfig = import ./home.nix { inherit pkgs; };
in
{
name = system;
value = {
home-manager = {
inherit pkgs;
config = { homeConfig = homeConfig; };
};
};
})
systems
);
};
}
This Flake defines two inputs: Nixpkgs and Home Manager. Nixpkgs is the default package repository for Nix, and Home Manager is a tool built on top of Nix for managing personal configurations.
The outputs section of the Flake defines the home-manager
output, which provides access to the Home Manager tool and your personal configurations. The home.nix
file referenced in this Flake will be created in the next step.
Now, you'll create a home.nix
file to configure Home Manager. This file will define your personal configurations, including shell settings, editor configurations, and other user-specific settings.
Create a new home.nixfile in the same directory as yourflake.nix
:
touch home.nix
In your home.nix
file, you'll define your personal configurations using the Nix configuration language. Here's an example:
{ pkgs, ... }:
{
home.username = "myusername";
programs.emacs = {
enable = true;
extraPackages = epkgs: [
epkgs.mu4e
epkgs.mu
epkgs.offlineimap
epkgs.isync
epkgs.notmuch
];
emacsConfig = ''
(setq user-full-name "My Name"
user-mail-address "[email protected]"
message-send-mail-function 'smtpmail-send-it
smtpmail-smtp-server "smtp.example.com"
smtpmail-smtp-service 587)
'';
};
programs.fish = {
enable = true;
interactiveShellInit = ''
set -x PATH $HOME/.nix-profile/bin $PATH
'';
};
# Add more configurations here...
}
This configuration sets up Emacs with mu4e, OfflineIMAP, and other email-related packages, and sets some global variables. It also sets up the Fish shell with a custom PATH variable.
Now that your Flake and Home Manager configurations are defined, you can install and activate them on your MacBook.
First, run the following command to install your Flake dependencies and generate a flake.lock
file:
nix build .#home-manager.activationPackage
This command will fetch the Nixpkgs and Home Manager inputs defined in your flake.nix
file, and generate a flake.lock
file to lock down their versions. It will build the specified output attribute and create a symlink called result
to the build results.
Next, run the following command to activate your Home Manager configurations:
./result/activate
This command will set up your personal configurations, including shell settings, editor configurations, and other user-specific settings.
You should now be able to use your MacBook with your Nix-managed configurations!
In this guide, we went over the key concepts and components of the Nix ecosystem, including Nix, Nixpkgs, Flakes, and Home Manager. We then walked through the process of setting up your MacBook with Nix and Home Manager using flakes, including defining a Flake, configuring Home Manager, and installing and activating your configurations.
By using Nix and Home Manager with flakes, you can manage your software dependencies and configurations in a declarative, reproducible, and versioned way, making it easy to set up and maintain your development environment.