From 67723c3ec1f5777d0ffa97370251995bc08c8081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Kugland?= Date: Wed, 13 Mar 2024 04:09:36 -0300 Subject: [PATCH] Added module virtualisation.qemu-user-static --- modules/default.nix | 1 + modules/qemu-user-static.nix | 59 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 modules/qemu-user-static.nix diff --git a/modules/default.nix b/modules/default.nix index ff6c7c0..e26d9c1 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -2,4 +2,5 @@ # Add your NixOS modules here # # my-module = ./my-module; + qemu-user-static = ./qemu-user-static.nix; } diff --git a/modules/qemu-user-static.nix b/modules/qemu-user-static.nix new file mode 100644 index 0000000..eca6816 --- /dev/null +++ b/modules/qemu-user-static.nix @@ -0,0 +1,59 @@ +{ + pkgs, + lib, + config, + ... +}: let + cfg = config.virtualisation.qemu-user-static; + + defaultImageParams = { + imageName = "docker.io/multiarch/qemu-user-static"; + finalImageTag = "7.2.0-1"; + imageDigest = "sha256:fe60359c92e86a43cc87b3d906006245f77bfc0565676b80004cc666e4feb9f0"; + sha256 = "sha256-eVBXjH7ltxM1Ojhub4gjqKYe64/2ySuEpCqBm3w7wfY="; + os = "linux"; + arch = "x86_64"; + }; +in { + options = { + virtualisation.qemu-user-static = { + enable = lib.mkEnableOption "qemu-user-static enables the execution of foreign architecture containers with QEMU and binfmt_misc (only available on x86_64-linux)"; + image = lib.mkOption { + type = lib.types.package; + default = pkgs.dockerTools.pullImage defaultImageParams; + description = '' + The image to use for qemu-user-static. + ''; + example = '' + pkgs.dockerTools.pullImage { + imageName = "docker.io/multiarch/qemu-user-static"; + finalImageTag = "7.2.0-1"; + imageDigest = "sha256:fe60359c92e86a43cc87b3d906006245f77bfc0565676b80004cc666e4feb9f0"; + sha256 = "sha256-eVBXjH7ltxM1Ojhub4gjqKYe64/2ySuEpCqBm3w7wfY="; + os = "linux"; + arch = "x86_64"; + } + ''; + }; + }; + }; + config = lib.mkIf cfg.enable { + virtualisation.oci-containers.containers.qemu-user-static = { + autoStart = true; + extraOptions = ["--rm" "--privileged"]; + cmd = ["--reset" "-p" "yes"]; + image = cfg.image.destNameTag; + imageFile = cfg.image; + }; + systemd.services.podman-qemu-user-static.serviceConfig = { + Type = lib.mkForce "oneshot"; + Restart = lib.mkForce "no"; + }; + assertions = [ + { + assertion = with config.nixpkgs.hostPlatform; (isx86_64 && isLinux); + message = "qemu-user-static is only available on x86_64-linux"; + } + ]; + }; +}