NixOS Configuration

Jan 14, 2024

#linux #nixos


NixOS is a fantastic distro when it comes into the safest system upgrade form one version to another.

Installation

Install nix by

# for multi user
sh <(curl -L https://nixos.org/nix/install) --daemon

Or, if you use Gentoo install it from nix-guix-gentoo overlay (follow link)

Configuration

NixOS entirely depends on its nix configuration files.

nix-channel

First add the nix-channel and update

nix-channel --add https://nixos.org/channels/nixpkgs-unstable
nix-channel --update

Removing a branch

nix-channel --remove <channel name>

nix-env

Installing package by nix-env

nix-env -iA nixpkgs.bat

Uninstalling a package

nix-env -e bat

Some openGL applications will throw error while executing

OpenGL wrapper

Use nixGL

nix-channel --add https://github.com/guibou/nixGL/archive/main.tar.gz nixgl && nix-channel --update
nix-env -iA nixgl.auto.nixGLDefault

and to succesfully open the program run

nixGL kitty

It does not create .desktop file into usr/share/applications/ or .local/share/applications so you need to simlink to (which $nix_path)

Garbage collection

nix-collect-garbage -d

nix-shell

To test a package in isolated manner

nix-shell -p nixpkgs.bat

Home manager

Easily maintain user packages and configuration files which has more programs and service option

For non NixOS system (standalone version)

nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager
nix-channel --update

and export the path

# nvim $HOME/.zshrc
export NIX_PATH=$HOME/.nix-defexpr/channels

and finally install by,

nix-shell '<home-manager>' -A install

Configuration file will be located in $HOME/.config/nixpkgs/home.nix
Install packages by home manager

home-manager build  # for testing
home-manage switch

Flake

Handles system update and management with better way

# add these in /etc/nixos/configuration.nix and run `nixos-rebuild build`
{ pkgs, ... }: {
  nix.settings.experimental-features = [ "nix-command" "flakes" ];
}

Make a directory e.g $HOME/flake, and cd into it and then run

nix flake init

to generate configuration file.

# FILE: flake.nix
{
  description = "A very basic flake";

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

  outputs = { self, nixpkgs, home-manager, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;
	config.allowUnfree = true;
      };
      lib = nixpkgs.lib;
    in {
      nixosConfigurations = {
        mynix = lib.nixosSystem {
          inherit system;
          modules = [
	    ./configuration.nix
	    home-manager.nixosModules.home-manager 
	    {
	      home-manager.useGlobalPkgs = true;
	      home-manager.useUserPackages = true;
	      home-manager.users.sanatan = import ./home.nix;
	    }
	  ];  
        };
      };
    };  
}

Now copy files /etc/nixos/* and $HOME/.config/nixpkgs/home.nix into flake directory

sudo nixos-rebuild switch --flake '#.mynix'

You can init git into this folder to keep track and host it on remote