品番

#011

26th Jan 2024 /

NixOS: A Developer-First Linux System

Introduction to Nix and NixOS

Nix is a powerful declarative package manager that enables users to define their system’s desired state through configuration files. The declarative approach means you specify what you want rather than how to achieve it. We’ll explore Nix’s capabilities in detail throughout this guide.

NixOS is a Linux-based operating system built on the Nix package manager. It embraces three core principles: declarative configuration, system reproducibility, and reliable system management.

NixOS Core Features

Declarative System Configuration

System Customization and Component Management

NixOS offers seamless system component management through its declarative configuration system:

Unlike traditional Linux distributions, where changing desktop environments often leads to dependency conflicts and leftover packages, NixOS manages these transitions cleanly. The system automatically handles:

For example, switching desktop environments requires only a simple configuration change:

kaneru@localhost:~$

# Reemplazas plasma5 por gnome xserver = { enable = true; desktopManager.plasma5 = { enable = true; }; }
Eva-01 from evangelion, in pixelart

System Rollback Capabilities

NixOS implements a robust system versioning mechanism that enables reliable system state restoration. During boot, GRUB presents a chronological list of system configurations, allowing you to:

Example GRUB menu:

kaneru@localhost:~$

# GNU GRUB VERSION 2.02 *NixOS - Configuration 129 (2024-01-23) *NixOS - Configuration 128 (2024-01-16) *NixOS - Configuration 127 (2023-11-28)
Eva-01 from evangelion, in pixelart

Dependency Resolution

Traditional package managers often struggle with dependency conflicts, where package installations or removals can break existing system components. Nix eliminates these conflicts through its unique architecture:

Active Community Ecosystem

The Nix ecosystem benefits from a vibrant and growing community:

Development Environments with Nix

The Nix ecosystem (Nix, NixOS, and nixpkgs) provides powerful tools for creating isolated, reproducible development environments. These environments ensure consistent development setups across teams and projects.

Key Benefits

Creating Development Environments

Development environments in Nix are typically defined using Nix Flakes, which provide:

Let’s explore how to create a development environment using a Flake:

kaneru@localhost:~$

{ description = "Example JavaScript development environment"; # Flake inputs inputs = { nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2305.491812.tar.gz"; }; # Flake outputs outputs = { self, nixpkgs }: let # Systems supported allSystems = [ "x86_64-linux" # 64-bit Intel/AMD Linux "aarch64-linux" # 64-bit ARM Linux "x86_64-darwin" # 64-bit Intel macOS "aarch64-darwin" # 64-bit ARM macOS ]; # Helper to provide system-specific attributes forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { pkgs = import nixpkgs { inherit system; }; }); in { # Development environment output devShells = forAllSystems ({ pkgs }: { default = pkgs.mkShell { # The Nix packages provided in the environment packages = with pkgs; [ nodejs_18 # Node.js 18, plus npm, npx, and corepack ]; }; }); }; }
Eva-01 from evangelion, in pixelart

Practical Example: Using Development Environments

Let’s demonstrate how to use a Nix development environment from a GitHub repository:

kaneru@localhost:~$

# Initialize a JavaScript development environment from a GitHub repository nix develop "github:linuxmobile/dotfiles#javascript"
Eva-01 from evangelion, in pixelart

Upon execution, you’ll enter a dedicated development shell:

kaneru@localhost:~$

(nix:linuxmobile-env) bash-5.1$
Eva-01 from evangelion, in pixelart

Verifying the isolated environment:

kaneru@localhost:~$

# Check Node.js installation path type node # Output shows isolated installation node is /nix/store/i88kh2fd03f5fsd3a948s19gliggd2wd-nodejs-18.12.1/bin/node
Eva-01 from evangelion, in pixelart

Language-Specific Environments

This approach extends to multiple programming languages and tools:

Version Management

Nix enables concurrent use of different versions: