Genode Packages collection https://git.sr.ht/~ehmry/genodepkgs/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

build-vms.nix 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. { system, localSystem, crossSystem
  2. # Nixpkgs, for qemu, lib and more
  3. , pkgs, modulesPath
  4. # NixOS configuration to add to the VMs
  5. , extraConfigurations ? [ ] }:
  6. with pkgs.lib;
  7. with import ./qemu-flags.nix { inherit pkgs; };
  8. rec {
  9. inherit pkgs;
  10. qemu = pkgs.buildPackages.buildPackages.qemu_test;
  11. # Build a virtual network from an attribute set `{ machine1 =
  12. # config1; ... machineN = configN; }', where `machineX' is the
  13. # hostname and `configX' is a NixOS system configuration. Each
  14. # machine is given an arbitrary IP address in the virtual network.
  15. buildVirtualNetwork = nodes:
  16. let nodesOut = mapAttrs (n: buildVM nodesOut) (assignIPAddresses nodes);
  17. in nodesOut;
  18. buildVM = nodes: configurations:
  19. import "${modulesPath}/../lib/eval-config.nix" {
  20. inherit system;
  21. modules = configurations ++ extraConfigurations;
  22. baseModules = (import "${modulesPath}/module-list.nix") ++ [
  23. ../../nixos-modules/genode-core.nix
  24. ../../nixos-modules/genode-init.nix
  25. ../../nixos-modules/qemu-vm.nix
  26. {
  27. key = "no-manual";
  28. documentation.nixos.enable = false;
  29. }
  30. {
  31. key = "qemu";
  32. system.build.qemu = qemu;
  33. }
  34. {
  35. key = "nodes";
  36. _module.args.nodes = nodes;
  37. }
  38. {
  39. system.build.qemu = qemu;
  40. nixpkgs = { inherit system crossSystem localSystem pkgs; };
  41. }
  42. ];
  43. };
  44. # Given an attribute set { machine1 = config1; ... machineN =
  45. # configN; }, sequentially assign IP addresses in the 192.168.1.0/24
  46. # range to each machine, and set the hostname to the attribute name.
  47. assignIPAddresses = nodes:
  48. let
  49. machines = attrNames nodes;
  50. machinesNumbered = zipLists machines (range 1 254);
  51. nodes_ = forEach machinesNumbered (m:
  52. nameValuePair m.fst [
  53. ({ config, nodes, ... }:
  54. let
  55. interfacesNumbered =
  56. zipLists config.virtualisation.vlans (range 1 255);
  57. interfaces = forEach interfacesNumbered ({ fst, snd }:
  58. nameValuePair "eth${toString snd}" {
  59. ipv4.addresses = [{
  60. address = "192.168.${toString fst}.${toString m.snd}";
  61. prefixLength = 24;
  62. }];
  63. });
  64. in {
  65. key = "ip-address";
  66. config = {
  67. networking.hostName = mkDefault m.fst;
  68. networking.interfaces = listToAttrs interfaces;
  69. networking.primaryIPAddress = optionalString (interfaces != [ ])
  70. (head (head interfaces).value.ipv4.addresses).address;
  71. # Put the IP addresses of all VMs in this machine's
  72. # /etc/hosts file. If a machine has multiple
  73. # interfaces, use the IP address corresponding to
  74. # the first interface (i.e. the first network in its
  75. # virtualisation.vlans option).
  76. networking.extraHosts = flip concatMapStrings machines (m':
  77. let config = (getAttr m' nodes).config;
  78. in optionalString (config.networking.primaryIPAddress != "")
  79. ("${config.networking.primaryIPAddress} "
  80. + optionalString (config.networking.domain != null)
  81. "${config.networking.hostName}.${config.networking.domain} "
  82. + ''
  83. ${config.networking.hostName}
  84. ''));
  85. virtualisation.qemu.options = forEach interfacesNumbered
  86. ({ fst, snd }: qemuNICFlags snd fst m.snd);
  87. };
  88. })
  89. (getAttr m.fst nodes)
  90. ]);
  91. in listToAttrs nodes_;
  92. }