switch to nixos-hardware

This commit is contained in:
Peder Bergebakken Sundt 2023-06-25 03:38:08 +02:00
parent 805e48523d
commit c2ee77a6cb
7 changed files with 285 additions and 248 deletions

View File

@ -1,30 +0,0 @@
{ config, lib, pkgs, modulesPath, ... }:
with lib;
{
system.build.nvmeImage = import "${modulesPath}/../lib/make-disk-image.nix" {
inherit config lib pkgs;
diskSize = 8*1024; # megabytes
# TODO: zstd
format = "raw"; # one of qcow2, qcow2-compressed, vdi, vpc, raw.
installBootLoader = true;
partitionTableType = "efi";
configFile = pkgs.writeText "configuration.nix" ''
{
imports = [ <./machine-config.nix> ];
}
'';
};
boot.growPartition = true;
boot.initrd.kernelModules = [ "nvme" ];
/** /
boot.loader.grub.efiSupport = true;
boot.loader.grub.efiInstallAsRemovable = true;
boot.loader.grub.device = "nodev";
/**/
fileSystems."/boot" = { device = "/dev/nvme0n1p1"; fsType = "vfat"; };
fileSystems."/" = { device = "/dev/nvme0n1p2"; fsType = "ext4"; };
}

View File

@ -1,21 +0,0 @@
{ config, lib, pkgs, modulesPath, ... }:
{
imports = [ "${modulesPath}/profiles/base.nix" ];
boot.enableContainers = false;
console.keyMap = "no";
programs.command-not-found.enable = false;
i18n.supportedLocales = [ "en_US.UTF-8/UTF-8" ];
documentation.man.enable = false;
documentation.info.enable = false;
security.polkit.enable = lib.mkForce false;
security.audit.enable = false;
system.stateVersion = "22.11";
environment.systemPackages = with pkgs;[
neofetch
fd
ripgrep
];
}

61
efi-image.nix Normal file
View File

@ -0,0 +1,61 @@
{ config, pkgs, lib, inputs, modulesPath, ... }:
# prior art:
# https://github.com/NixOS/nixos-hardware/blob/master/starfive/visionfive/v2/sd-image.nix
# https://github.com/nix-community/nixos-generators/blob/master/formats/raw.nix
# https://github.com/nix-community/nixos-generators/blob/master/formats/raw-efi.nix
let
firmware = pkgs.callPackage "${inputs.nixos-hardware}/starfive/visionfive/v2/firmware.nix" { };
in {
options.efiImage = {
imageBaseName = lib.mkOption {
default = "nixos-efi-image";
description = lib.mdDoc "Prefix of the name of the generated image file.";
};
};
config = {
boot.loader.grub.device = config.fileSystems.${config.boot.loader.efi.efiSysMountPoint}.device; # hack for installBootLoader
fileSystems.${config.boot.loader.efi.efiSysMountPoint}.neededForBoot = true;
fileSystems."/".autoResize = true;
boot.growPartition = true;
#boot.loader.grub.device = "nodev";
#boot.loader.grub.efiSupport = true;
#boot.loader.grub.efiInstallAsRemovable = true;
system.build.efiImage = import "${modulesPath}/../lib/make-disk-image.nix" {
inherit config lib pkgs;
name = "${config.efiImage.imageBaseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}-starfive-visionfive2.img";
#diskSize = 8*1024; # megabytes, defaults to "auto"
bootSize = "512M"; # nixos-install default
partitionTableType = "efi";
installBootLoader = true;
touchEFIVars = true;
#efiFirmware
#efiVariables
fsType = config.fileSystems."/".fsType;
# TODO: zstd
format = "raw"; # one of qcow2, qcow2-compressed, vdi, vpc, raw.
# TODO: make this thing able to rebuild
#configFile = pkgs.writeText "configuration.nix" ''
# {
# imports = [ <./machine-config.nix> ];
# }
#'';
};
assertions = [
{
assertion = config.boot.loader.systemd-boot.enable;
message = "Building EFI Image requires systemd-boot";
}
];
};
}

View File

@ -1,116 +1,41 @@
{
"nodes": {
"meta-sifive": {
"flake": false,
"nixos-hardware": {
"locked": {
"lastModified": 1674637034,
"narHash": "sha256-63ND5ppiTZq6R0S5DQXzKwTdP+ksRN2xUBSnOuSZIVc=",
"owner": "sifive",
"repo": "meta-sifive",
"rev": "0cf45af2861140a11a3655b6c1185a1b781420a0",
"lastModified": 1686838567,
"narHash": "sha256-aqKCUD126dRlVSKV6vWuDCitfjFrZlkwNuvj5LtjRRU=",
"owner": "nixos",
"repo": "nixos-hardware",
"rev": "429f232fe1dc398c5afea19a51aad6931ee0fb89",
"type": "github"
},
"original": {
"owner": "sifive",
"ref": "master",
"repo": "meta-sifive",
"type": "github"
}
},
"nickcao-rv64": {
"inputs": {
"meta-sifive": "meta-sifive",
"nixpkgs": [
"nixpkgs"
],
"starfive-tools": "starfive-tools",
"u-boot-starfive": "u-boot-starfive",
"uboot-vf2-src": "uboot-vf2-src"
},
"locked": {
"lastModified": 1678100894,
"narHash": "sha256-P4zsTLZeD+jZW7wdTa+gIEqaOKV5atcpiaRMMSPVyWw=",
"owner": "NickCao",
"repo": "nixos-riscv",
"rev": "a95622d7c532f0bd6271bbc937d8c55faf1f4801",
"type": "github"
},
"original": {
"owner": "NickCao",
"repo": "nixos-riscv",
"owner": "nixos",
"repo": "nixos-hardware",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1678101150,
"narHash": "sha256-Y0EAwuMqucZrH7FwkAeDAXSOIEFlQGVoQSk3+Nj07Ls=",
"owner": "NickCao",
"lastModified": 1687502512,
"narHash": "sha256-dBL/01TayOSZYxtY4cMXuNCBk8UMLoqRZA+94xiFpJA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "d23ade162e3a0fd23479a7073eeac9da5f1a40db",
"rev": "3ae20aa58a6c0d1ca95c9b11f59a2d12eebc511f",
"type": "github"
},
"original": {
"owner": "NickCao",
"ref": "riscv",
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nickcao-rv64": "nickcao-rv64",
"nixos-hardware": "nixos-hardware",
"nixpkgs": "nixpkgs"
}
},
"starfive-tools": {
"flake": false,
"locked": {
"lastModified": 1674126575,
"narHash": "sha256-dKRHFWLJrXIEpsuXOO3zpiIplPFG5ghVLYR8GvBat8M=",
"owner": "NickCao",
"repo": "starfive-tools",
"rev": "7038f61fcd684fc60d2abc4fd95909782480c66b",
"type": "github"
},
"original": {
"owner": "NickCao",
"repo": "starfive-tools",
"type": "github"
}
},
"u-boot-starfive": {
"flake": false,
"locked": {
"lastModified": 1651224393,
"narHash": "sha256-48ah4mcEqrK9iRAz/QGCZUcz34xAuZyJuPanQ8IssMU=",
"owner": "NickCao",
"repo": "u-boot-starfive",
"rev": "ac75aa54020412a83b61dad46c5ea15e7f9f525c",
"type": "github"
},
"original": {
"owner": "NickCao",
"repo": "u-boot-starfive",
"type": "github"
}
},
"uboot-vf2-src": {
"flake": false,
"locked": {
"lastModified": 1678096112,
"narHash": "sha256-w6dPaok72sW7Q4ChSWDqds+djWWwigirz9f+s623r/g=",
"owner": "NickCao",
"repo": "u-boot-starfive",
"rev": "64fc719a5db536abaec42564689d96bcfdf0dfba",
"type": "github"
},
"original": {
"owner": "NickCao",
"ref": "visionfive2",
"repo": "u-boot-starfive",
"type": "github"
}
}
},
"root": "root",

244
flake.nix
View File

@ -1,99 +1,171 @@
{
description = "My visionfive 2 setup";
inputs = {
nixpkgs.url = "github:NickCao/nixpkgs/riscv"; # https://github.com/NickCao/nixpkgs/tree/riscv
#nixpkgs.url = "github:nixos/nixpkgs/nixos-22.11"; # https://github.com/nixos/nixpkgs/tree/nixos-22.11
#unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable"; # https://github.com/nixos/nixpkgs/tree/nixpkgs-unstable
nickcao-rv64.url = "github:NickCao/nixos-riscv"; # https://github.com/NickCao/nixos-riscv
nickcao-rv64.inputs.nixpkgs.follows = "nixpkgs";
#nickcao-rv64.url = "path:../nickcao-nixos-riscv"; # broken: https://github.com/NixOS/nix/issues/3978
#zhao-rv64.url = "github:zhaofengli/nixos-riscv64"; # https://github.com/zhaofengli/nixos-riscv64
};
#inputs.nixpkgs.url = "github:NickCao/nixpkgs/riscv"; # https://github.com/NickCao/nixpkgs/tree/riscv
#inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.nixos-hardware.url = "github:nixos/nixos-hardware";
outputs = { self, nixpkgs, nickcao-rv64, ...}@inputs:
#inputs.nickcao-rv64.url = "github:NickCao/nixos-riscv"; # https://github.com/NickCao/nixos-riscv
#inputs.nickcao-rv64.inputs.nixpkgs.follows = "nixpkgs";
#inputs.zhao-rv64.url = "github:zhaofengli/nixos-riscv64"; # https://github.com/zhaofengli/nixos-riscv64
outputs = {
self,
nixpkgs,
nixos-hardware,
#nickcao-rv64,
...} @ inputs:
let
nixlib = nixpkgs.lib;
overlays = nixlib.mapAttrsToList (name: val: val) self.overlays;
hostname = hostname: {
networking.hostName = hostname;
networking.domain = "pbsds.net";
};
#systems = nixpkgs.lib.systems.flakeExposed;
systems = [
"x86_64-linux"
"aarch64-linux"
"riscv64-linux"
];
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f rec {
inherit system;
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) liv;
mkNixos = modules: nixpkgs.lib.nixosSystem { inherit system modules; };
vf2-firmware = pkgs.callPackage "${nixos-hardware}/starfive/visionfive/v2/firmware.nix" { };
});
#overlays = nixpkgs.lib.mapAttrsToList (name: val: val) self.overlays;
in {
inherit inputs;
#hydraJobs = {}
overlays = {
nickcao = nickcao-rv64.overlay;
#default = (final: prev: {});
};
#overlays = {
# nickcao = nickcao-rv64.overlay;
# #default = (final: prev: {});
#};
nixosModules.generic-boot = {
boot.loader.grub.enable = false;
boot.loader.generic-extlinux-compatible.enable = true;
};
nixosModules.rv64 = {
system = "riscv64-unknown-linux-gnu";
#nixpkgs.overlays = [ self.overlays.nickcao ];
};
nixosModules.rv64-cross = {
nixpkgs.localSystem.config = "x86_64-unknown-linux-gnu";
nixpkgs.crossSystem.config = "riscv64-unknown-linux-gnu";
#nixpkgs.overlays = [ self.overlays.nickcao ];
};
nixosModules.nvme-image = { config, lib, pkgs, modulesPath , ... }: {
system.build.nvmeImage = import "${modulesPath}/../lib/make-disk-image.nix" {
inherit config lib pkgs;
diskSize = 8*1024; # megabytes
# TODO: zstd
format = "raw"; # one of qcow2, qcow2-compressed, vdi, vpc, raw.
installBootLoader = true;
partitionTableType = "efi";
# TODO: make this thing able to rebuild
configFile = pkgs.writeText "configuration.nix" ''
{
imports = [ <./machine-config.nix> ];
}
'';
nixosModules = rec {
# hardware
rv64 = { config, lib, ... }: lib.mkIf (config.nixpkgs.system != "riscv64-linux") {
# (system != "riscv64-linux") => do cross
nixpkgs.crossSystem.config = "riscv64-unknown-linux-gnu";
nixpkgs.crossSystem.system = "riscv64-linux";
};
vf2-r12a = { lib, ...}: {
imports = [ rv64 ];
hardware.deviceTree.name = "starfive/jh7110-starfive-visionfive-2-v1.2a.dtb";
};
vf2-r12a-8gb = {
imports = [ vf2-r12a ];
hardware.deviceTree.overlays = [{
name = "8GB-patch";
dtsFile = "${nixos-hardware}/starfive/visionfive/v2/visionfive-2-v1.2a-8GB.dts";
}];
};
vf2-r13b = { lib, ...}: {
imports = [ rv64 ];
hardware.deviceTree.name = "starfive/jh7110-starfive-visionfive-2-v1.3b.dtb"; # default
};
vf2-r13b-8gb = {
imports = [ vf2-r13b ];
hardware.deviceTree.overlays = [{
name = "8GB-patch";
dtsFile = "${nixos-hardware}/starfive/visionfive/v2/visionfive-2-v1.3b-8GB.dts";
}];
};
# install method
vf2-qemu = { lib, ...}: {
imports = [ "${nixos-hardware}/starfive/visionfive/v2" ./qemu.nix ];
};
vf2-sd-installer = {
imports = [ "${nixos-hardware}/starfive/visionfive/v2/sd-image-installer.nix" ];
sdImage.compressImage = false;
};
vf2-sd-image = {
imports = [ "${nixos-hardware}/starfive/visionfive/v2/sd-image.nix" ];
sdImage.compressImage = false;
};
vf2-nvme = { vf2-firmware, pkgs, ... }: {
imports = [ "${nixos-hardware}/starfive/visionfive/v2" ];
#fileSystems."/boot" = { device = "/dev/nvme0n1p1"; fsType = "vfat"; };
#fileSystems."/" = { device = "/dev/nvme0n1p2"; fsType = "ext4"; };
boot.initrd.kernelModules = [ "nvme" ];
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
boot.loader.efi.efiSysMountPoint = "/boot";
environment.systemPackages = [
pkgs.efibootmgr
pkgs.efivar
vf2-firmware.updater-flash
];
};
vf2-nvme-image = {
_module.args = { inherit inputs; }; # TODO: needed?
imports = [ vf2-nvme ./efi-image.nix ];
};
# components
minimal = { lib, ...}: {
boot.enableContainers = false;
programs.command-not-found.enable = false;
documentation.man.enable = false;
documentation.info.enable = false;
security.polkit.enable = lib.mkForce false;
security.audit.enable = false;
networking.firewall.enable = lib.mkDefault false;
};
dhcp = {
networking.interfaces.end0.useDHCP = true;
networking.interfaces.end1.useDHCP = true;
networking.domain = "pbsds.net";
networking.search = [ "pbsds.net" ];
};
locale-no_nb = {
console.keyMap = "no";
services.xserver.layout = "no";
services.xserver.xkbVariant = "";
time.timeZone = "Europe/Oslo";
i18n.defaultLocale = "en_US.utf8";
i18n.extraLocaleSettings.LC_TIME = "nb_NO.utf8";
};
# hosts
demo = { pkgs, ... }: {
imports = [ minimal dhcp "${nixpkgs}/nixos/modules/profiles/base.nix" ];
networking.hostName = "demo";
system.stateVersion = "23.05";
};
asgaut = { pkgs, ... }: {
imports = [ minimal dhcp locale-no_nb ];
networking.hostName = "asgaut";
environment.systemPackages = with pkgs;[ neofetch htop fd ripgrep ];
system.stateVersion = "23.05";
};
gunder = { pkgs, ... }: {
imports = [ minimal dhcp locale-no_nb ];
networking.hostName = "gunder";
environment.systemPackages = with pkgs;[ neofetch htop fd ripgrep ];
system.stateVersion = "23.05";
};
boot.growPartition = true;
boot.initrd.kernelModules = [ "nvme" ];
/** /
boot.loader.grub.efiSupport = true;
boot.loader.grub.efiInstallAsRemovable = true;
boot.loader.grub.device = "nodev";
/**/
fileSystems."/boot" = { device = "/dev/nvme0n1p1"; fsType = "vfat"; };
fileSystems."/" = { device = "/dev/nvme0n1p2"; fsType = "ext4"; };
};
nixosModules.sd-image = { config, lib, pkgs, modulesPath, ... }: {
imports = [ "${modulesPath}/installer/sd-card/sd-image.nix" ];
sdImage.populateFirmwareCommands = "";
sdImage.populateRootCommands = ''
mkdir -p ./files/boot
${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot
'';
};
nixosModules.nickcao-common = "${nickcao-rv64.outPath}/common.nix"; # is equal to: generic-boot + sd-image + rv64-cross
nixosModules.nickcao-unmatched = "${nickcao-rv64.outPath}/unmatched.nix";
nixosModules.nickcao-visionfive = "${nickcao-rv64.outPath}/visionfive.nix";
nixosModules.nickcao-visionfive2 = "${nickcao-rv64.outPath}/visionfive2.nix";
nixosModules.nickcao-qemu = "${nickcao-rv64.outPath}/qemu.nix";
nixosModules.base = {
nixpkgs.overlays = overlays;
imports = [ ./configuration.nix ];
};
# TODO: gunder
# nixos-rebuild --flake .#NAME
nixosConfigurations.qemu = nickcao-rv64.nixosConfigurations.qemu.extendModules { modules = with self.nixosModules; [ base ]; };
nixosConfigurations.asgaut = nixpkgs.lib.nixosSystem { modules = with self.nixosModules; [ base (hostname "asgaut") nickcao-visionfive2 ]; };
nixosConfigurations.asgaut-nvme = nixpkgs.lib.nixosSystem { modules = with self.nixosModules; [ base (hostname "asgaut") nickcao-visionfive2 generic-boot rv64-cross nvme-image ]; };
nixosConfigurations.asgaut-sd = nixpkgs.lib.nixosSystem { modules = with self.nixosModules; [ base (hostname "asgaut") nickcao-visionfive2 generic-boot rv64-cross sd-image ]; };
nixosConfigurations.asgaut-sd2 = nixpkgs.lib.nixosSystem { modules = with self.nixosModules; [ base (hostname "asgaut") nickcao-visionfive2 nickcao-common ]; };
# nix build .#images.NAME .
images.asgaut-nvme = self.nixosConfigurations.asgaut-nvme.config.system.build.nvmeImage;
images.asgaut-sd = self.nixosConfigurations.asgaut-sd .config.system.build.sdImage;
images.asgaut-sd2 = self.nixosConfigurations.asgaut-sd2 .config.system.build.sdImage;
# nix build .#riscv64-linux.NAME-sd-image
#packages.riscv64-linux.asgaut-sd-image = self.nixosConfigurations.gunder.config.system.build.sdImage;
packages = forAllSystems ({ system, mkNixos, ... }: with self.nixosModules; rec {
#default = asgaut-sd-installer;
demo-sd-installer = (mkNixos [ demo vf2-r12a-8gb vf2-sd-installer ]).config.system.build.sdImage;
demo-sd-image = (mkNixos [ demo vf2-r12a-8gb vf2-sd-image ]).config.system.build.sdImage;
demo-nvme-image = (mkNixos [ demo vf2-r12a-8gb vf2-nvme-image ]).config.system.build.efiImage;
demo-qemu = (mkNixos [ demo rv64 vf2-qemu ]).config.system.build.vm;
asgaut-sd-installer = (mkNixos [ asgaut vf2-r12a-8gb vf2-sd-installer ]).config.system.build.sdImage;
asgaut-sd-image = (mkNixos [ asgaut vf2-r12a-8gb vf2-sd-image ]).config.system.build.sdImage;
asgaut-nvme-image = (mkNixos [ asgaut vf2-r12a-8gb vf2-nvme-image ]).config.system.build.efiImage;
asgaut-qemu = (mkNixos [ asgaut rv64 vf2-qemu ]).config.system.build.vm;
gunder-sd-installer = (mkNixos [ gunder vf2-r12a-8gb vf2-sd-installer ]).config.system.build.sdImage;
gunder-sd-image = (mkNixos [ gunder vf2-r12a-8gb vf2-sd-image ]).config.system.build.sdImage;
gunder-nvme-image = (mkNixos [ gunder vf2-r12a-8gb vf2-nvme-image ]).config.system.build.efiImage;
gunder-qemu = (mkNixos [ gunder rv64 vf2-qemu ]).config.system.build.vm;
});
nixosConfigurations.demo = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ demo vf2-nvme ]; };
nixosConfigurations.asgaut = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ asgaut vf2-nvme ]; };
nixosConfigurations.gunder = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ gunder vf2-nvme ]; };
};
}

View File

@ -1,21 +0,0 @@
{ config, pkgs, lib, modulesPath, ... }: {
nixpkgs = {
localSystem.config = "x86_64-unknown-linux-gnu";
crossSystem.config = "riscv64-unknown-linux-gnu";
};
boot.loader = {
grub.enable = false;
generic-extlinux-compatible.enable = true;
};
sdImage = {
populateFirmwareCommands = "";
populateRootCommands = ''
mkdir -p ./files/boot
${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot
'';
};
}

51
qemu.nix Normal file
View File

@ -0,0 +1,51 @@
# based on https://github.com/NickCao/nixos-riscv/blob/master/qemu.nix
{ config, pkgs, lib, ...}:
{
boot.loader.generic-extlinux-compatible.enable = false;
#boot.kernelPackages = pkgs.linuxPackages_latest;
boot.kernelParams = [ "console=ttyS0" "earlycon" ];
boot.initrd.availableKernelModules = [ "pci_host_generic" "virtio_pci" "9p" "9pnet_virtio" ];
fileSystems."/" = {
device = "none";
fsType = "tmpfs";
options = [ "defaults" "mode=755" ];
};
fileSystems."/nix/store" = {
device = "nix-store";
fsType = "9p";
options = [ "ro" "trans=virtio" "version=9p2000.L" "msize=1M" ];
};
systemd.services."autotty@hvc0".enable = false;
systemd.services.mount-pstore.enable = false;
services.getty.autologinUser = "root";
networking.firewall.enable = false;
#virtualisation.docker.enable = true;
system.build.vm =
let
qemu-path = "${pkgs.pkgsBuildBuild.qemu}/bin/qemu-system-${pkgs.targetPlatform.qemuArch}";
closure = config.system.build.toplevel;
qemu-args = [
"-M" "virt"
"-smp" "2" # cpus
"-m" "2G" # mem
"-kernel" "${closure}/kernel"
"-initrd" "${closure}/initrd"
"-append" "$(cat ${closure}/kernel-params) init=${closure}/init"
"-device" "virtio-rng-pci"
"-netdev" "user,id=end0"
"-device" "virtio-net-pci,netdev=end0"
"-netdev" "user,id=end1"
"-device" "virtio-net-pci,netdev=end1"
"-fsdev" "local,security_model=passthrough,id=nix-store,path=/nix/store,readonly=on"
"-device" "virtio-9p-pci,id=nix-store,fsdev=nix-store,mount_tag=nix-store"
"-nographic"
];
in pkgs.writeShellScriptBin "run-vm" ''
exec ${qemu-path} ${lib.escapeShellArgs qemu-args}
'';
}