Creating a NixOS live CD: Difference between revisions

From NixOS Wiki
imported>Makefu
m How-To -> Guide
imported>HLandau
No edit summary
Line 1: Line 1:
==Motivation==
Creating a modified NixOS LiveCD out of an existing working NixOS installation has a number of benefits:
* Ensures authenticity.
* No need for internet access.
* It is easy to add your own packages and configuration changes to the image.


== motivation ==
==Building==
creating a modified NIXOS live CD out of a working nixos installation gives you the benefits:
* verify what is included
* no need to download it
* it is easy to add your own programs to the image
 
== how to build your own image ==
 
Create a file "iso.nix":


Create a file <tt>iso.nix</tt>:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# This module defines a small NixOS installation CD.  It does not
# This module defines a small NixOS installation CD.  It does not
Line 17: Line 15:
   imports = [
   imports = [
     <nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix>
     <nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix>
     # Provide an initial copy of the NixOS channel so that the user
     # Provide an initial copy of the NixOS channel so that the user
     # doesn't need to run "nix-channel --update" first.
     # doesn't need to run "nix-channel --update" first.
Line 32: Line 31:
The resulting image can be found in <code>result</code>:
The resulting image can be found in <code>result</code>:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
ls result/iso/
$ ls result/iso/
nixos-17.09.git.158ec57-x86_64-linux.iso
nixos-17.09.git.158ec57-x86_64-linux.iso
</syntaxhighlight>
</syntaxhighlight>




=== Start SSH with your SSH key ===
===SSH===
 
In your <tt>iso.nix</tt>:
In your iso.nix:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# enable ssh in the iso boot process
{
systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ];
  ...
users.users.root.openssh.authorizedKeys.keys [
  # Enable SSH in the boot process.
  "ssh-ed25519 AaAeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee username@host"
  systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ];
];
  users.users.root.openssh.authorizedKeys.keys [
    "ssh-ed25519 AaAeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee username@host"
  ];
  ...
}
</syntaxhighlight>
</syntaxhighlight>


=== Add static ip addresses ===
===Static IP Address===


The following snippet is useful, when static ip addresses are required, e.g. for VPS installation
Static IP addresses can be set in the image itself. This can be useful for VPS installation.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
networking = {
{
  usePredictableInterfaceNames = false;
  ...
  interfaces.eth0.ip4 = [{
  networking = {
    address = "64.137.201.46";
    usePredictableInterfaceNames = false;
    prefixLength = 24;
    interfaces.eth0.ip4 = [{
  }];
      address = "64.137.201.46";
  defaultGateway = "64.137.201.1";
      prefixLength = 24;
  nameServers = [ "8.8.8.8" ];
    }];
};
    defaultGateway = "64.137.201.1";
    nameServers = [ "8.8.8.8" ];
  };
  ...
}
</syntaxhighlight>
</syntaxhighlight>




=== ZFS support ===
===ZFS Support===


The default iso does not contain zfs tools and drivers by default. This can be changed by:
NixOS ISOs do not contain ZFS tools and kernel modules by default. They can be added via:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
boot.supportedFilesystems = [ "zfs" ];
{
  ...
  boot.supportedFilesystems = [ "zfs" ];
  ...
}
</syntaxhighlight>
</syntaxhighlight>


in iso.nix
==See also==
 
* [https://nixos.org/nixos/manual/index.html#sec-building-cd NixOS Manual: Building your own CD].
== software installation inside the 'once' deployed and booted image ==
in case you have booted from your image you can add software as described here:
* [[install/remove software]]
 
== references ==
* See also section "Building your own NixOS CD" of the [https://nixos.org/nixos/manual/index.html#sec-building-cd NixOS manual].


[[Category:NixOS]]
[[Category:NixOS]]

Revision as of 20:48, 24 October 2017

Motivation

Creating a modified NixOS LiveCD out of an existing working NixOS installation has a number of benefits:

  • Ensures authenticity.
  • No need for internet access.
  • It is easy to add your own packages and configuration changes to the image.

Building

Create a file iso.nix:

# This module defines a small NixOS installation CD.  It does not
# contain any graphical stuff.
{config, pkgs, ...}:
{
  imports = [
    <nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix>

    # Provide an initial copy of the NixOS channel so that the user
    # doesn't need to run "nix-channel --update" first.
    <nixpkgs/nixos/modules/installer/cd-dvd/channel.nix>
  ];
}

Build the image via:

nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=iso.nix

The resulting image can be found in result:

$ ls result/iso/
nixos-17.09.git.158ec57-x86_64-linux.iso


SSH

In your iso.nix:

{
  ...
  # Enable SSH in the boot process.
  systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ];
  users.users.root.openssh.authorizedKeys.keys [
    "ssh-ed25519 AaAeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee username@host"
  ];
  ...
}

Static IP Address

Static IP addresses can be set in the image itself. This can be useful for VPS installation.

{
  ...
  networking = {
    usePredictableInterfaceNames = false;
    interfaces.eth0.ip4 = [{
      address = "64.137.201.46";
      prefixLength = 24;
    }];
    defaultGateway = "64.137.201.1";
    nameServers = [ "8.8.8.8" ];
  };
  ...
}


ZFS Support

NixOS ISOs do not contain ZFS tools and kernel modules by default. They can be added via:

{
  ...
  boot.supportedFilesystems = [ "zfs" ];
  ...
}

See also