home

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

qemu.nix (2025B)


      1 { config, pkgs, lib, ... }:
      2 
      3 with lib;
      4 let
      5   cfg = config.profiles.qemu-user;
      6   arm = {
      7     interpreter = "${pkgs.qemu-user-arm}/bin/qemu-arm";
      8     magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'';
      9     mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
     10   };
     11   aarch64 = {
     12     interpreter = "${pkgs.qemu-user-arm64}/bin/qemu-aarch64";
     13     magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'';
     14     mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
     15   };
     16   riscv64 = {
     17     interpreter = "${pkgs.qemu-riscv64}/bin/qemu-riscv64";
     18     magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'';
     19     mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
     20   };
     21 in
     22 {
     23   options = {
     24     profiles.qemu-user = {
     25       arm = mkEnableOption "enable 32bit arm emulation";
     26       aarch64 = mkEnableOption "enable 64bit arm emulation";
     27       riscv64 = mkEnableOption "enable 64bit riscv emulation";
     28     };
     29     nix.supportedPlatforms = mkOption {
     30       type = types.listOf types.str;
     31       description = "extra platforms that nix will run binaries for";
     32       default = [ ];
     33     };
     34   };
     35   config = mkIf (cfg.arm || cfg.aarch64) {
     36     nixpkgs = {
     37       overlays = [ (import ../../../overlays/qemu/default.nix) ];
     38     };
     39     boot.binfmt.registrations =
     40       optionalAttrs cfg.arm { inherit arm; }
     41       // optionalAttrs cfg.aarch64 { inherit aarch64; }
     42       // optionalAttrs cfg.riscv64 { inherit riscv64; };
     43     nix.supportedPlatforms = (optionals cfg.arm [ "armv6l-linux" "armv7l-linux" ])
     44       ++ (optional cfg.aarch64 "aarch64-linux");
     45     nix.extraOptions = ''
     46       extra-platforms = ${toString config.nix.supportedPlatforms} i686-linux
     47     '';
     48     nix.sandboxPaths = [ "/run/binfmt" ] ++ (optional cfg.arm "${pkgs.qemu-user-arm}") ++ (optional cfg.aarch64 "${pkgs.qemu-user-arm64}");
     49   };
     50 }