Skip to content

Latest commit

 

History

History
243 lines (209 loc) · 7.79 KB

nix.org

File metadata and controls

243 lines (209 loc) · 7.79 KB

Nix Fresh Install guide

rsc/Nix.svg

Table of Content

Nix

Introduction

  1. Just like NixOS, it supports declarative reproductible system configurations using home-manager.
  2. Install packages at user level without having to change system state.
  3. SELinux is currently unsupported. You need to disable it.
  4. Primary used on systemd but can be used on other init systems. You will have to enable the nix daemon manually.

Getting Started

Nix and home-manager Website

Nix

Home-Manager

Installing Nix

This command will install the Nix Package Manager on your system. More information can be found here. When prompted, allow the installer to use root priviliges and to set up Nix as a Multi User.

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

Installing Packages

If you just want to use the Nix Package Manager, great, you are done. You can install new packages using the command below. Available packages can be found here:

  • $ nix-env -iA nixpkgs.<package name>
  • $ nix-shell -p <package name>

A couple more useful commands:

  • $ nix-env --uninstall <package name>
  • $ nix-env -q
  • $ nix-env --upgrade

If you would also like to configure these packages using their options, it’s recommended that you keep reading.

Garbage Collection

  • Remove undeclared packages, dependencies and symlinks:
    • $ sudo nix-collect-garbage -d

Installation

  • Home-manager can be installed by using the commands below:

Setup

Initial

As a user

  • Add the channel:
    • $ nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager
    • $ nix-channel --add https://github.com/nix-community/home-manager/archive/release-21.11.tar.gz home-manager
  • $ nix-channel --update
  • Just to be sure, relog.

Installation

  • If installation give NIX-PATH errors
    • $ sudo nix-collect-garbage -d
    • $ export NIX_PATH=$HOME/.nix-defexpr/channels:/nix/var/nix/profiles/per-user/root/channels${NIX_PATH:+:$NIX_PATH}
  • Installation:
    • $ nix-shell '<home-manager>' -A install
  • Configuration file:
    • $ cd ~/.config/nixpkgs/home.nix

Configuration

Links

Declare user packages

home.packages = with pkgs; [
  firefox
];

services.dunst = {
  enable = true;
};

Applying

  • $ home-manager switch

Flakes

Introduction

  • Flakes are an “upcoming feature” of the Nix package manager.
  • Specify code dependencies declaratively (will be stored in flake.lock)
    • For example: home-manager
  • Rebuilding and updating whole system made easy
  • Very useful tool to build your own config
    • Multiple configs in one
    • People with github dotfiles will feel right at home

Getting Started

Flakes Wiki

Preparing the System

Allowing experimental features such as flake to be installed

$ mkdir -p ~/.config/nix
$ echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf

Installation

Generate

This command will generate a flake.nix and flake.lock file

  • cd into a location to store in your system
  • $ nix flake init
{
  description = "A very basic flake";

  outputs = { self, nixpkgs}: {
    packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
  };
}

Inputs and Outputs

Inputs

attribute set of all the dependencies used in the flake

inputs = {
  nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  home-manager = {
    url = "github:nix-community/home-manager";
    inputs.nixpkgs.follows = "nixpkgs";
  };
  nixgl = {
    url = "gihub:guibou/nixGL";
    inputs.nixpkgs.follows = "nixpkgs";
  };
};
Outputs

function of an argument that uses the inputs for reference

  • Configure what you imported
  • Can be pretty much anything: Packages / configurations / modules / etc…

Configuration

inputs = {
  nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
  home-manager = {
    url = "github:nix-community/home-manager";
    inputs.nixpkgs.follows = "nixpkgs";
  };
};
outputs = { self, nixpkgs, home-manager, ...}: {
  let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
  in
  homeConfigurations  = {
    "<host>" = home-manager.lib.homeManagerConfiguration {
      inherit pkgs;
      extraSpecialArgs = { inherit inputs; };
      modules = [
        ./<path.nix>
        {
          home = {
            username = "<user>";
            homeDirectory = "/home/${user}";
            packages = [ pkgs.home-manager ];
            stateVersion = "22.05";
          };
        }
      ];
    };
    #"<second host>" = home-manager.lib.homeManagerConfiguration {
    #  pkgs = nixpkgs.legacyPackages."x86_64-linux";
    #  extraSpecialArgs = { inherit inputs; };
    #  modules = [
    #    ./<path.nix>
    #    {
    #      home = {
    #        ...
   };
};

First build

This is only for those who don’t have nix-darwin installed and have an existing flake they want to install on a fresh system If this is not your situation, move on to rebuild

  • For the first initial installation it recommended that your use $ nix build
  • The location of /result depends on what location you are building from. It’s maybe recommended that your build inside the flake.
$ cd <flake>
$ nix build .#homeConfigurations.<host>.activationPackage
$ ./result/activate

Rebuild

  • After the first installation, you don’t need to target /activate inside /result
  • $ home-manager is now installed and can be used from anywhere. Example:
    • /HOME/<USER>/ $ home-manager switch --flake <flake path>#<host>

Uninstall

Nix Package Manager

  • Full guide
  • Commands are based on a systemd distribution, but I guess can be modified for your distro of choice.
$ sudo systemctl stop nix-daemon.socket
$ sudo systemctl stop nix-daemon.service
$ sudo systemctl disable nix-daemon.socket
$ sudo systemctl disable nix-daemon.service
$ sudo systemctl daemon-reload
  • reboot

Resources

  1. NixOS Website
  2. NixOS Learn
  3. Nix Manual
  4. NixOS Wiki
  5. Nix Pills
  6. Home-Manager Github
  7. Home-Manager Manual
  8. Home-Manager Appendix_A
  9. Home-Manager Appendix B