From fe19c91c6b0293441aca084e88a60ee59640922c Mon Sep 17 00:00:00 2001 From: Sander Date: Wed, 22 Nov 2023 14:53:08 +0000 Subject: [PATCH] feat: enable KVM on Linux if available --- README.md | 2 ++ action.yml | 5 +++++ install-nix.sh | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index c446da36..ae1bbee4 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,8 @@ To install Nix from any commit, go to [the corresponding installer_test action]( - `nix_path`: set `NIX_PATH` environment variable, for example `nixpkgs=channel:nixos-unstable` +- `enable_kvm`: whether to enable KVM for hardware-accelerated virtualization on Linux. Enabled by default if available. + --- ## FAQ diff --git a/action.yml b/action.yml index 9bd8e053..d33114a0 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,10 @@ inputs: description: 'Additional installer flags passed to the installer script.' nix_path: description: 'Set NIX_PATH environment variable.' + enable_kvm: + description: 'Enable KVM for hardware-accelerated virtualization on Linux, if available.' + required: false + default: true branding: color: 'blue' icon: 'sun' @@ -26,4 +30,5 @@ runs: INPUT_INSTALL_OPTIONS: ${{ inputs.install_options }} INPUT_INSTALL_URL: ${{ inputs.install_url }} INPUT_NIX_PATH: ${{ inputs.nix_path }} + INPUT_ENABLE_KVM: ${{ inputs.enable_kvm }} GITHUB_TOKEN: ${{ github.token }} diff --git a/install-nix.sh b/install-nix.sh index 4462f3a7..c5265a49 100755 --- a/install-nix.sh +++ b/install-nix.sh @@ -6,6 +6,17 @@ if nix_path="$(type -p nix)" ; then exit fi +if [[ ($OSTYPE =~ linux) && ($INPUT_ENABLE_KVM == 'true') ]]; then + enable_kvm() { + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-install-nix-action-kvm.rules + sudo udevadm control --reload-rules && sudo udevadm trigger --name-match=kvm + } + + echo '::group::Enabling KVM support' + enable_kvm && echo 'Enabled KVM' || echo 'KVM is not available' + echo '::endgroup::' +fi + # GitHub command to put the following log messages into a group which is collapsed by default echo "::group::Installing Nix"