我正在使用 microvm 创建并运行 NixOS 虚拟机(在安装了 nix 数据包管理器的 Debian 系统上)。
VM 内的 NIX_PATH 环境变量设置为其默认值:
nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
上面引用的 nixpkgs 路径在虚拟机上不存在,
/nix/var/nix/profiles/per-user/
为空。
如何配置虚拟机,以便使用对也用于构建虚拟机的 nixpkgs 通道的工作引用来构建虚拟机?
这些问题看起来很相似,但我还不确定:
我的 flake.nix 用于构建 microvm:
{
description = "NixOS in MicroVMs";
inputs.microvm.url = "github:astro/microvm.nix";
inputs.microvm.inputs.nixpkgs.follows = "nixpkgs";
outputs = { self, nixpkgs, microvm }:
let
system = "x86_64-linux";
user = "REDACTED";
vmname = "haskell";
in {
defaultPackage.${system} = self.packages.${system}.my-microvm;
packages.${system} = {
my-microvm = self.nixosConfigurations.my-microvm.config.microvm.declaredRunner;
};
nixosConfigurations = {
my-microvm = nixpkgs.lib.nixosSystem {
inherit system;
modules = [
microvm.nixosModules.microvm
({pkgs, ... }: {
environment.systemPackages = [
pkgs.ghc
pkgs.git
(pkgs.haskell-language-server.override { supportedGhcVersions = [ "94" ]; })
pkgs.stack
];
microvm = {
forwardPorts = [
{ from = "host"; host.port = 2222; guest.port = 22; }
];
hypervisor = "qemu";
interfaces = [
{ type = "user"; id = "usernet"; mac = "00:00:00:00:00:02"; }
];
mem = 4096;
shares = [ {
proto = "9p";
tag = "ro-store";
source = "/nix/store";
mountPoint = "/nix/.ro-store";
} {
proto = "virtiofs";
tag = "hometest";
source = "~/.local/share/microvm/vms/${vmname}/shares/home";
mountPoint = "/home/${user}";
socket = "/run/user/1000/microvm-virtiofsd_vm_home";
}
];
socket = "/run/user/1000/microvm-control.socket";
vcpu = 3;
volumes = [];
writableStoreOverlay = "/home/${user}/nix-rw-store";
};
networking.hostName = vmname;
nix.settings = {
extra-experimental-features = ["nix-command" "flakes"];
trusted-users = [user];
};
security.sudo = {
enable = true;
wheelNeedsPassword = false;
};
services.getty.autologinUser = user;
services.openssh = {
enable = true;
};
users.users.${user} = {
extraGroups = [ "wheel" "video" ];
group = "user";
isNormalUser = true;
openssh.authorizedKeys.keys = [
"ssh-rsa REDACTED"
];
password = "";
};
users.users.root.password = "";
users.groups.user = {};
})
];
};
};
};
}
事实证明,我真正需要的是在虚拟机内进行
<nixpkgs>
解析。我通过在虚拟机配置中设置此选项来实现这一点:
nix.nixPath = ["nixpkgs=${builtins.storePath <nixpkgs>}"];
然后用
nix run --impure
运行薄片。不纯是必要的,因为builtins.storePath是从外部输入的。
我也在这里讨论了这个问题:https://github.com/astro/microvm.nix/issues/175