NixOS: Opt In State - Immutable Linux Installation

Download NixOS

Reddit discussion

NixOS with ZFS NixOS with btrfs

Devices

GPD MicroPC

Link

In order to make the keyboard available during Stage 1 boot, where the LUKS passphrase is entered, the battery kernel module must be available. Add it by appending "battery" to boot.initrd.availableKernelModules in the nix configuration.

Enable thermald and TLP.

services.thermald.enable = true;
services.tlp.enable = true;

Installation

NixOS with btrfs

Stage I

2: EFI 1: LUKS; BTRFS (swap as a file ArchWiki)

sudo su
WHOLEDISK=...............
DISK=....................
parted "$WHOLEDISK" -- mklabel gpt
parted "$WHOLEDISK" -- mkpart primary 512MiB 100%
parted "$WHOLEDISK" -- mkpart ESP fat32 1MiB 512MiB
parted "$WHOLEDISK" -- set 2 esp on
mkfs.vfat -n boot "$DISK"2
cryptsetup -v luksFormat "$DISK"1
cryptsetup open "$DISK"1 RootFS
mkfs.btrfs /dev/mapper/RootFS
mount -t btrfs /dev/mapper/RootFS /mnt
for x in root home nix persist log swap; do
  btrfs subvolume create /mnt/$x
done
btrfs subvolume snapshot -r /mnt/root /mnt/root-blank
umount /mnt
mount -o subvol=root,noatime,nodiratime /dev/mapper/RootFS /mnt
for x in home nix persist swap; do
  mkdir /mnt/$x
  mount -o subvol=$x,noatime,nodiratime /dev/mapper/RootFS /mnt/$x
done
mkdir -p /mnt/var/log
mount -o subvol=log,noatime,nodiratime /dev/mapper/RootFS /mnt/var/log
mkdir /mnt/boot
mount "$DISK"2 /mnt/boot
cd /mnt/swap
truncate -s 0 ./swapfile
chattr +C ./swapfile
btrfs property set ./swapfile compression none
dd if=/dev/zero of=./swapfile bs=1M count=4096 status=progress
chmod 600 ./swapfile
mkswap ./swapfile
swapon ./swapfile
cd
nixos-generate-config --root /mnt
nano /mnt/etc/nixos/hardware-configuration.nix # add `neededForBoot = true;` in /var/log
nano /mnt/etc/nixos/configuration.nix
nixos-install

Reboot and configure desktop environment

Stage II

Security Through Isolation

A: nixos-container

nixos-container

Consider adding -U option. Declarative NixOS containers

GUI Apps - attempt A: xpra only

inspired by subuser

Failed attempt
  networking.nat.enable = true;
  networking.nat.internalInterfaces = [ "ve-+" ];
  networking.nat.externalInterface = "eth0";
  networking.networkmanager.unmanaged = [ "interface-name:ve-*" ];

  containers.xpraServer = {
    ephemeral = true;
    autoStart = true;
  };
  containers.xpraServer.config = {config, pkgs, ...}: {
    environment.systemPackages = with pkgs; [ xpra ];
    users.users.user = {
      isNormalUser = true;
    };
    systemd.services.xpraServer = {
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        Type = "simple";
        User = "user";
        ExecStart = ''${pkgs.xpra}/bin/xpra --no-daemon start :7'';
      };
    };
    networking.firewall.allowedTCPPorts = [ 6007 ];
  };
  systemd.user.services.xpraClient = {
    wantedBy = [ "default.target" ];
    serviceConfig = {
      Type = "simple";
      ExecStart = ''${pkgs.xpra}/bin/xpra attach tcp:localhost:7'';
    };
  };

  containers.firefox.config = {config, pkgs, ...}: {
    environment.systemPackages = with pkgs; [ firefox gnome3.gedit wget file xfce.terminal gnome3.nautilus xpra ];
    users.users.user = {
      isNormalUser = true;
    };
  };
GUI Apps - attempt B: ssh with Xpra

Tor Browser in a Container

let
  createContainer = containerConfig: {
    autoStart = false;
    privateNetwork = true;
    hostAddress = "192.168.7." + toString containerConfig.id;
    localAddress = "192.168.7." + toString (containerConfig.id + 1);
    config = { config, pkgs, ... }: let raw = containerConfig.config { config = config; pkgs = pkgs; }; in raw // {
      environment = ({ systemPackages = {}; } // raw).environment // { systemPackages = [ pkgs.xpra ] ++ raw.environment.systemPackages; };
      users.users.user = {
        isNormalUser = true;
        openssh.authorizedKeys.keys = [ SSH-KEYS-GO-HERE ];
        extraGroups = ["audio" "video"];
      };
      services = ({ services = {}; } // raw).services // { openssh = {
        enable = true;
        forwardX11 = true;
      }; };
    };
  };
in { config, pkgs, ... }:
{
  networking.firewall.allowedTCPPorts = [ 4713 6000 ];
  hardware.pulseaudio = {
    enable = true;
    systemWide = true;
    support32Bit = true;
    tcp = { enable = true; anonymousClients = { allowedIpRanges = ["127.0.0.1" "192.168.7.0/24"]; }; };
  };
  networking.nat.enable = true;
  networking.nat.internalInterfaces = [ "ve-+" ];
  networking.nat.externalInterface = "eth0";
  networking.networkmanager.unmanaged = [ "interface-name:ve-*" ];
........... (please install xpra)

script

#!/bin/sh
CONTAINER=...
APP=...
sudo nixos-container stop "$CONTAINER"
sudo nixos-container start "$CONTAINER"
exec xpra --opengl=yes --bandwidth-limit=0 --pulseaudio=yes --compression_level=0 --encoding=rgb --terminate-children=yes start ssh://user@"$(nixos-container show-ip $CONTAINER)"/ --border=red --start-child="$APP"

Final Configuration File

configuration.nix (please replace ________________________________________________________________________________________________)

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  # Use the systemd-boot EFI boot loader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;
  boot.kernelPackages = pkgs.linuxPackages_latest;
  boot.supportedFilesystems = [ "btrfs" ];
  hardware.enableAllFirmware = true;
  nixpkgs.config.allowUnfree = true;

  swapDevices = [ { device = "/swap/swapfile"; size = 4096; } ];

  networking.hostName = "________________________________________________________________________________________________"; # Define your hostname.
  networking.networkmanager.enable = true;
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  # Set your time zone.
  time.timeZone = "________________________________________________________________________________________________";

  # The global useDHCP flag is deprecated, therefore explicitly set to false here.
  # Per-interface useDHCP will be mandatory in the future, so this generated config
  # replicates the default behaviour.
  networking.useDHCP = false;
  networking.interfaces.eno1.useDHCP = true;
  networking.interfaces.wlp1s0.useDHCP = true;

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # Select internationalisation properties.
  i18n.defaultLocale = "en_US.UTF-8";
  # console = {
  #   font = "Lat2-Terminus16";
  #   keyMap = "us";
  # };

  # Enable the X11 windowing system.
  services.xserver.enable = true;

  # Enable the Plasma 5 Desktop Environment.
  services.xserver.displayManager.sddm.enable = true;
  services.xserver.displayManager.autoLogin.enable = true;
  services.xserver.displayManager.autoLogin.user = "user";
  services.xserver.desktopManager.plasma5.enable = true;
  nixpkgs.config.firefox.enablePlasmaBrowserIntegration = true;
  powerManagement.enable = true;

  # Configure keymap in X11
  services.xserver.layout = "us";
  # services.xserver.xkbOptions = "eurosign:e";

  # Enable CUPS to print documents.
  # services.printing.enable = true;

  # Enable sound.
  sound.enable = true;
  hardware.pulseaudio.enable = true;

  hardware.cpu.intel.updateMicrocode = true;

  hardware.opengl.enable = true;

  services.thermald.enable = true;
  services.tlp.enable = true;

  services.chrony.enable = true;

  # Enable touchpad support (enabled default in most desktopManager).
  services.xserver.libinput.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.user = {
    isNormalUser = true;
    extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
  };

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
    brise # rime
    wget file
  ];

  i18n.inputMethod = {
    enabled = "fcitx";
    fcitx.engines = with pkgs.fcitx-engines; [ rime libpinyin ];
  };

  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  # services.openssh.enable = true;

  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "20.09"; # Did you read the comment?

}

Download - 20.09

Tsinghua mirror Official sha256sum