spacepaste

  1.  
  2. {
  3. mountScript ? "/bin/sh -i;",
  4. pkgs ? (import <nixpkgs> {}),
  5. kernelPackages ? pkgs: pkgs.linuxPackagesFor pkgs.linux_latest,
  6. udevPackages ? pkgs: [],
  7. kernelModulePackages ? kp: [],
  8. firmwarePackages ? pkgs: [pkgs.firmwareLinuxNonfree],
  9. tools ? pkgs: [],
  10. modprobeConfig ? "",
  11. qemu ? pkgs: pkgs.kvm,
  12. qemuArgs ? "--enable-kvm -m 2G -bios ${pkgs.OVMF.fd}/FV/OVMF.fd"
  13. }:
  14. rec {
  15. maybeCall = f: arg: if builtins.isFunction f then f arg else f;
  16. kernel = (maybeCall kernelPackages pkgs).kernel;
  17. bzImage = kernel + "/bzImage";
  18. firmwareSet = pkgs.buildEnv {
  19. name = "firmware-set";
  20. paths = maybeCall firmwarePackages pkgs;
  21. pathsToLink = ["/lib/firmware"];
  22. };
  23. _kernelPackages = maybeCall kernelPackages pkgs;
  24. _kernelModulePackages = [_kernelPackages.kernel] ++
  25. maybeCall kernelModulePackages _kernelPackages;
  26. kernelModules = pkgs.aggregateModules _kernelModulePackages;
  27. _udevPackages = maybeCall udevPackages pkgs ++ [
  28. pkgs.eudev pkgs.lvm2 pkgs.libinput
  29. ];
  30. udevRules = pkgs.runCommand "udev-rules" {} ''
  31. mkdir -p "$out"/etc/udev/{rules.d,hwdb.d}
  32. for i in ${toString _udevPackages}; do
  33. test -d "$i"/etc/udev/rules.d/ && ln -sf "$i"/etc/udev/rules.d/* "$out"/etc/udev/rules.d
  34. test -d "$i"/var/lib/udev/rules.d/ && ln -sf "$i"/var/lib/udev/rules.d/* "$out"/etc/udev/rules.d
  35. test -d "$i"/etc/udev/hwdb.d/ && ln -sf "$i"/etc/udev/hwdb.d/* "$out"/etc/udev/hwdb.d
  36. test -d "$i"/var/lib/udev/hwdb.d/ && ln -sf "$i"/var/lib/udev/hwdb.d/* "$out"/etc/udev/hwdb.d
  37. done
  38. ${pkgs.eudev}/bin/udevadm hwdb -u -r "$out"
  39. '';
  40. modprobeConf = pkgs.writeText "modprobe.conf" ''
  41. blacklist evbug
  42. '' + modprobeConfig;
  43. initTools = pkgs.buildEnv {
  44. name = "init-tools";
  45. paths =
  46. (with pkgs; [
  47. busybox utillinux kmod bashInteractive lvm2 cryptsetup coreutils
  48. gnugrep gnused eudev
  49. ])
  50. ++ (maybeCall tools pkgs);
  51. ignoreCollisions = true;
  52. pathsToLink = ["/"];
  53. extraOutputsToInstall = ["bin" "out"];
  54. };
  55. initScript = ''
  56. export PATH="/init-tools/bin"
  57. mkdir -p /new-root /proc /sys /dev /run /tmp /etc
  58. mount proc -t proc /proc
  59. mount sysfs -t sysfs /sys
  60. mount run-tmpfs -t tmpfs /run
  61. mount dev-tmpfs -t devtmpfs /dev
  62. mkdir -p /dev/shm /dev/pts /run/user /run/lock /run/nix
  63. mount shm -t tmpfs /dev/shm
  64. mount /devpts -t devpts /dev/pts
  65. chmod a+rwt /dev/shm
  66. echo -n /init-tools/bin/modprobe > /proc/sys/kernel/modprobe
  67. echo -n /firmware > /sys/module/firmware_class/parameters/path
  68. ln -sfT /proc/mounts /etc/mtab
  69. touch /etc/fstab
  70. echo "Ready to start udev"
  71. udevd &
  72. udevadm trigger --action=add
  73. udevadm settle
  74. vgchange -ay
  75. udevadm trigger --action=add
  76. udevadm settle
  77. ${mountScript}
  78. cd /new-root/
  79. udevadm control -e
  80. lsmod
  81. mkdir -p ./{proc,sys,dev,run}
  82. mount --move /proc ./proc
  83. mount --move /sys ./sys
  84. mount --move /dev ./dev
  85. mount --move /run ./run
  86. echo "Ready for switch_root"
  87. exec switch_root . ./"$targetInit"
  88. '';
  89. init = pkgs.writeScript "init" ("#!/bin/sh\n" + initScript);
  90. initrd = pkgs.makeInitrd {
  91. contents = [
  92. { object = init; symlink = "/init";}
  93. { object = kernelModules + "/lib/modules"; symlink = "/lib/modules"; }
  94. { object = initTools; symlink = "/init-tools"; }
  95. { object = pkgs.busybox; symlink = "/busybox"; }
  96. { object = initTools + "/bin/bash"; symlink = "/bin/sh"; }
  97. { object = initTools + "/bin/env"; symlink = "/usr/bin/env"; }
  98. { object = firmwareSet + "/lib/firmware"; symlink = "/firmware"; }
  99. { object = modprobeConf; symlink = "/etc/modprobe.d/modprobe.conf"; }
  100. { object = udevRules + "/etc/udev"; symlink = "/etc/udev"; }
  101. ];
  102. };
  103. qemuScript = pkgs.writeScript "qemu-script" ''#! /bin/sh
  104. ${maybeCall qemu pkgs}/bin/qemu-system-$(echo "${builtins.currentSystem}" | sed -e "s/-.*//") \
  105. ${qemuArgs} -kernel ${bzImage} -initrd ${initrd}/initrd "$@"
  106. '';
  107. qemuScriptTextMode = pkgs.writeScript "qemu-script" ''#! /bin/sh
  108. ${maybeCall qemu pkgs}/bin/qemu-system-$(echo "${builtins.currentSystem}" | sed -e "s/-.*//") \
  109. ${qemuArgs} -kernel ${bzImage} -initrd ${initrd}/initrd \
  110. -nographic -append "console=ttyS0" "$@"
  111. '';
  112. }
  113.