Skip to content

Instantly share code, notes, and snippets.

@jmettes
Last active June 4, 2021 06:09
Show Gist options
  • Select an option

  • Save jmettes/abc57a018be70efed9876f5529ff8a1c to your computer and use it in GitHub Desktop.

Select an option

Save jmettes/abc57a018be70efed9876f5529ff8a1c to your computer and use it in GitHub Desktop.

Revisions

  1. jmettes revised this gist Oct 28, 2019. 1 changed file with 7 additions and 13 deletions.
    20 changes: 7 additions & 13 deletions iso.nix
    Original file line number Diff line number Diff line change
    @@ -9,19 +9,13 @@

    # add the automated installation files
    environment.etc = {
    "install.sh" = {
    source = ./install.sh;
    mode = "0700";
    };
    "configuration.nix" = {
    source = ./configuration.nix;
    };
    "hardware-configuration.nix" = {
    source = ./hardware-configuration.nix;
    };
    "closure-nix-store-path.txt" = {
    source = ./closure-nix-store-path.txt;
    };
    "install.sh" = {
    source = ./install.sh;
    mode = "0700";
    };
    "closure-nix-store-path.txt" = {
    source = ./closure-nix-store-path.txt;
    };
    "system" = {
    source = ./system;
    };
  2. jmettes created this gist Oct 28, 2019.
    5 changes: 5 additions & 0 deletions build-closure.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    nix-build --attr system "./nixos.nix" -o result-closure
    readlink -f result-closure > closure-nix-store-path.txt
    rm -r system
    mkdir system
    nix copy ./result-closure --to file://./system
    1 change: 1 addition & 0 deletions build-iso.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=iso.nix -o result-iso
    44 changes: 44 additions & 0 deletions configuration.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    { config, pkgs, ... }:

    {
    imports =
    [
    ./hardware-configuration.nix
    ];

    boot.loader.grub.device = "/dev/sda";

    services.openssh = {
    enable = true;
    permitRootLogin = "no";
    };

    networking = {
    defaultGateway = {
    address = "192.168.1.1";
    interface = "enp3s0";
    };

    interfaces.enp3s0 = {
    useDHCP = false;
    ipv4.addresses = [{
    address = "192.168.1.2";
    prefixLength = 24;
    }];
    };

    nameservers = ["8.8.8.8" "8.8.4.4"];
    };

    users.users.myuser = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
    password = "mypassword";
    };

    environment.systemPackages = [
    pkgs.vim
    ];

    system.stateVersion = "19.03";
    }
    18 changes: 18 additions & 0 deletions hardware-configuration.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    { config, lib, pkgs, ... }:

    {
    imports = [ ];

    boot.initrd.availableKernelModules = [ "ata_piix" "ohci_pci" "sd_mod" "sr_mod" ];
    boot.initrd.kernelModules = [ ];
    boot.kernelModules = [ ];
    boot.extraModulePackages = [ ];

    fileSystems."/" =
    { device = "/dev/disk/by-label/nixos";
    fsType = "ext4";
    };

    swapDevices = [ ];
    nix.maxJobs = lib.mkDefault 1;
    }
    18 changes: 18 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    echo "create MBR partition"
    parted --script /dev/sda -- mklabel msdos

    echo "create root partition"
    parted --script -a opt /dev/sda mkpart primary ext4 0% 100%

    echo "format partition"
    mkfs.ext4 -L nixos /dev/sda1 -F
    sleep 1

    echo "mount target filesystem"
    mount /dev/disk/by-label/nixos /mnt

    echo "copy closure to nix store"
    nix copy --from file:///etc/system $(cat /etc/closure-nix-store-path.txt) --option binary-caches "" --no-check-sigs

    echo "install nix"
    nixos-install --no-root-passwd --option binary-caches "" --system $(cat /etc/closure-nix-store-path.txt)
    37 changes: 37 additions & 0 deletions iso.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    { config, pkgs, ... }:

    {
    imports = [
    # https://nixos.wiki/wiki/Creating_a_NixOS_live_CD
    <nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix>
    <nixpkgs/nixos/modules/installer/cd-dvd/channel.nix>
    ];

    # add the automated installation files
    environment.etc = {
    "install.sh" = {
    source = ./install.sh;
    mode = "0700";
    };
    "configuration.nix" = {
    source = ./configuration.nix;
    };
    "hardware-configuration.nix" = {
    source = ./hardware-configuration.nix;
    };
    "closure-nix-store-path.txt" = {
    source = ./closure-nix-store-path.txt;
    };
    "system" = {
    source = ./system;
    };
    };

    # automatically run install script
    environment.etc."profile.local".text = ''
    /etc/install.sh
    reboot
    '';


    }
    17 changes: 17 additions & 0 deletions nixos.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # http://www.haskellforall.com/2018/08/nixos-in-production.html
    let
    nixpkgs = builtins.fetchTarball {
    url = "https://github.com/NixOS/nixpkgs/archive/b74b1cdb2fecc31ff7a127c5bc89771f887c93bb.tar.gz";
    sha256 = "0ncr4g29220amqm4riaa1xf4jz55v2nmh9fi16f1gzhww1gplk8h";
    };

    in
    import "${nixpkgs}/nixos" {
    configuration = {
    imports = [
    ./configuration.nix
    ];
    };

    system = "x86_64-linux";
    }