diff --git a/configuration.nix b/configuration.nix new file mode 100644 index 0000000..7de1cde --- /dev/null +++ b/configuration.nix @@ -0,0 +1,54 @@ +# This is a small config that can be used to bootstrap a system with ZFS. +{ config, lib, modulesPath, ... }: +{ + imports = + [ + (modulesPath + "/installer/scan/not-detected.nix") + ]; + + # Generate a hostId for ZFS + # head -c4 /dev/urandom | od -A none -t x4 + networking.hostId = "f35c6293"; + networking.hostName = "MiniNix"; + boot = { + initrd.availableKernelModules = [ "xhci_pci" "ahci" "nvme" "usbhid" "usb_storage" "sd_mod" ]; + initrd.kernelModules = [ ]; + kernelModules = [ "kvm-intel" ]; + extraModulePackages = [ ]; + loader = { + grub = { + enable = true; + zfsSupport = true; + device = "nodev"; + mirroredBoots = [ + { devices = [ "nodev" ]; path = "/boot"; } + ]; + }; + }; + }; + + fileSystems."/" = { + device = "zroot/root"; + fsType = "zfs"; + }; + + fileSystems."/nix" = { + device = "zroot/nix"; + fsType = "zfs"; + }; + + fileSystems."/var" = { + device = "zroot/var"; + fsType = "zfs"; + }; + + fileSystems."/home" = { + device = "zroot/home"; + fsType = "zfs"; + }; + + swapDevices = [ ]; + + hardware.cpu.intel.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware; + system.stateVersion = "24.05"; +} diff --git a/disko-nixos.nix b/disko-nixos.nix new file mode 100644 index 0000000..1c566bc --- /dev/null +++ b/disko-nixos.nix @@ -0,0 +1,73 @@ +{ disks ? [ "/dev/sda" ], ... }: { + disko.devices = { + disk = { + main = { + type = "disk"; + device = builtins.elemAt disks 0; + content = { + type = "gpt"; + partitions = { + ESP = { + size = "1024M"; + type = "EF00"; + content = { + type = "filesystem"; + format = "vfat"; + mountpoint = "/boot"; + }; + }; + zfs = { + size = "100%"; + content = { + type = "zfs"; + pool = "zroot"; + }; + }; + }; + }; + }; + }; + zpool = { + zroot = { + type = "zpool"; + mode = ""; + rootFsOptions = { + compression = "zstd"; + "com.sun:auto-snapshot" = "false"; + mountpoint = "none"; + acltype = "posixacl"; + }; + mountOptions = [ + "ashift=12" + ]; + + postCreateHook = "zfs list -t snapshot -H -o name | grep -E '^zroot@blank$' || zfs snapshot zroot@blank"; + + datasets = { + root = { + type = "zfs_fs"; + mountpoint = "/"; + }; + + nix = { + type = "zfs_fs"; + mountpoint = "/nix"; + }; + + var = { + type = "zfs_fs"; + mountpoint = "/var"; + }; + + home = { + type = "zfs_fs"; + mountpoint = "/home"; + options = { + "com.sun:auto-snapshot" = "true"; + }; + }; + }; + }; + }; + }; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ba24c5f --- /dev/null +++ b/flake.lock @@ -0,0 +1,48 @@ +{ + "nodes": { + "disko": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1720056646, + "narHash": "sha256-BymcV4HWtx2VFuabDCM4/nEJcfivCx0S02wUCz11mAY=", + "owner": "nix-community", + "repo": "disko", + "rev": "64679cd7f318c9b6595902b47d4585b1d51d5f9e", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "disko", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1720110830, + "narHash": "sha256-E5dN9GDV4LwMEduhBLSkyEz51zM17XkWZ3/9luvNOPs=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "c0d0be00d4ecc4b51d2d6948e37466194c1e6c51", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "disko": "disko", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..6bf79d8 --- /dev/null +++ b/flake.nix @@ -0,0 +1,28 @@ +{ + description = "MiniNix"; + + inputs = { + # Nixpkgs + nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05"; + + # disko - Declarative disk partitioning and formatting using nix + disko = { + url = "github:nix-community/disko"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { self, nixpkgs, disko, ... }@inputs: { + nixosConfigurations = { + "mini-x86" = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + specialArgs = { inherit inputs; }; + modules = [ + ./configuration.nix + disko.nixosModules.disko + (import ./disko-nixos.nix { disks = [ "/dev/nvme0n1" ]; }) + ]; + }; + }; + }; +}