NixOS system configuration: Difference between revisions

m minor fix of typo
Pandapip1 (talk | contribs)
m Right, that also needs buildPackages
 
(7 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[NixOS]] uses a declarative configuration system that allows users to manage their entire operating system setup including installed packages, system services, user accounts, hardware settings, and more through configuration files. This page serves as an overview of how to work with and manage NixOS system configurations.
[[NixOS]] uses a declarative configuration system that allows users to manage their entire operating system setup including installed packages, system services, user accounts, hardware settings, and more thorough configuration files. This page serves as an overview of how to work with and manage NixOS system configurations.


For an introduction to declarative configuration, see the [[Overview of the NixOS Linux distribution#Declarative Configuration]] and the {{NixOS Manual|name=NixOS official manual|anchor=#ch-configuration}}.
For an introduction to declarative configuration, see the [[Overview of the NixOS Linux distribution#Declarative Configuration]] and the {{NixOS Manual|name=NixOS official manual|anchor=#ch-configuration}}.
Line 59: Line 59:
== Defining NixOS as a flake ==
== Defining NixOS as a flake ==


[[Flakes]] offer an alternative, modern way to configure NixOS systems using a standardized and reproducible structure. With flakes, the system configuration — along with any additional inputs such as Nixpkgs or external modules — is managed declaratively within a <code>flake.nix</code> file.
[[Flakes]] offer an alternative way to configure NixOS systems using a standardized and reproducible structure. With flakes, the system configuration — along with any additional inputs such as Nixpkgs or external modules — is managed declaratively within a <code>flake.nix</code> file.


Unlike the traditional configuration model, which relies on <code>/etc/nixos/configuration.nix</code> and channels, flakes explicitly define their dependencies and configurations, making it easier to share, version, and reproduce complete NixOS system setups.
Unlike the traditional configuration model, which relies on <code>/etc/nixos/configuration.nix</code> and channels, flakes explicitly define their dependencies and configurations, making it easier to share, version, and reproduce complete NixOS system setups.
Line 70: Line 70:
<nowiki>
<nowiki>
{
{
   inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
   inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
   outputs = { self, nixpkgs }: {
   outputs = { self, nixpkgs }: {
     # replace 'joes-desktop' with your hostname here.
     # replace 'joes-desktop' with your hostname here.
Line 87: Line 87:
{{file|flake.nix|nix|<nowiki>
{{file|flake.nix|nix|<nowiki>
{
{
   inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
   inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
   inputs.home-manager.url = github:nix-community/home-manager;
   inputs.home-manager.url = "github:nix-community/home-manager";


   outputs = { self, nixpkgs, ... }@inputs: {
   outputs = { self, nixpkgs, ... }@inputs: {
Line 180: Line 180:
==== Importing packages from multiple nixpkgs branches ====
==== Importing packages from multiple nixpkgs branches ====


A NixOS config flake could be as follows (replace <hostname> with your hostname):
A NixOS config flake could be as follows (replace hostname with your hostname):


{{file|3=<nowiki>
{{file|3=<nowiki>
Line 187: Line 187:


  inputs = {
  inputs = {
     nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
     nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
     nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
     nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
   };
   };
Line 194: Line 194:
     { nixpkgs, nixpkgs-unstable, ... }:
     { nixpkgs, nixpkgs-unstable, ... }:
     {
     {
       nixosConfigurations."<hostname>" = nixpkgs.lib.nixosSystem {
       nixosConfigurations."hostname" = nixpkgs.lib.nixosSystem {
         modules = [
         modules = [
           {
           {
             nixpkgs.overlays = [
             nixpkgs.overlays = [
               (final: prev: {
               (final: prev: {
                 unstable = nixpkgs-unstable.legacyPackages.${prev.system};
                 unstable = import nixpkgs-unstable {
                # use this variant if unfree packages are needed:
                  inherit (final) config;
                # unstable = import nixpkgs-unstable {
                  inherit (final.stdenv.hostPlatform) system;
                #  inherit prev;
                 };
                #  system = prev.system;
                 #  config.allowUnfree = true;
                # };
               })
               })
             ];
             ];
Line 260: Line 257:
       path = pkgs.path;
       path = pkgs.path;
       narHash = builtins.readFile
       narHash = builtins.readFile
           (pkgs.runCommandLocal "get-nixpkgs-hash"
           (pkgs.buildPackages.runCommandLocal "get-nixpkgs-hash"
             { nativeBuildInputs = [ pkgs.nix ]; }
             { nativeBuildInputs = [ pkgs.buildPackages.nix ]; }
             "nix-hash --type sha256 --sri ${pkgs.path} > $out");
             "nix-hash --type sha256 --sri ${pkgs.path} > $out");
     };
     };
   };
   };
</nowiki>
</nowiki>
}}
|name=|lang=}}


This has the unfortunate side-effect of requiring import-from-derivation and slowing down build times, however it may greatly speed up almost every eval. Full-time flakes users may be able to just use <code>narHash = pkgs.narHash</code>.
This has the unfortunate side-effect of requiring import-from-derivation and slowing down build times, however it may greatly speed up almost every eval. Full-time flakes users may be able to just use <code>narHash = pkgs.narHash</code>.
Line 274: Line 271:
How to get a nix repl out of your system flake:
How to get a nix repl out of your system flake:


<syntaxhighlight lang="text">
<syntaxhighlight lang="nix">
$ nix repl
$ nix repl