Skip to content

Directories in make-disk-image contents end up in a subdirectory of target #226203

@leon-barrett

Description

@leon-barrett

Describe the bug

make-disk-image takes an argument contents that copies source paths into the resulting VM image. It seems to work fine when the sources are files or pkgs.writeText, but when copying directories, they end up nested inside the target directory named with a nix-store-like path. This is not what I expected, it is hard to use, and I do not think it is the intended behavior.

Steps To Reproduce

  1. Create these files:
# demo.nix
{ nixpkgs ? import <nixpkgs> {  } }:
let
  lib = nixpkgs.lib;
  pkgs = nixpkgs.pkgs;
  system = "x86_64-linux";
  makeDiskImage = import "${pkgs.path}/nixos/lib/make-disk-image.nix";
  evalConfig = import "${pkgs.path}/nixos/lib/eval-config.nix";
  image = {
    lib = pkgs.lib;
    pkgs = pkgs;
    config = (evalConfig { inherit system; inherit pkgs; modules = [ ./nixos/configuration.nix ]; }).config;
    name = "test NixOS image";
    format = "qcow2";
    contents = [
      { source = ./directory; target = "/directory"; }
    ];
  };
in {
  image = makeDiskImage image;
}
# directory/file
contents

(and additional configuration.nix, see attached demo.tar.gz).
2. Build the VM image with nix-build demo.nix.
3. Run the VM image, e.g. with cp -f --no-preserve=mode result/nixos.qcow2 . && virt-install --name=nixos --vcpus=4 --memory=4096 --import --disk nixos.qcow2 --os-variant=nixos-unstable.
4. Log in with credentials literal user:password.
5. Note that /directory contains a subdirectory with a Nix-store-like path, e.g. /directory/frz...-directory instead of file.

Expected behavior

I expected that the directory source would end up at the /directory target, as happens with contents objects that are files.

Screenshots

screenshot-2023-04-14_15-25

Additional context

I will have a PR with a test and fix up soon.

When changing the behavior of a function that is already widely used, even fixing a bug can cause problems for people. I checked, but I did not find any existing usages of make-disk-image with directories in nixpkgs. And given how broken the directory support seems, I suspect that it is not actually used in the wild, so I think it is okay to fix this behavior.

Notify maintainers

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 5.19.0-38-generic, Ubuntu, 22.04.2 LTS (Jammy Jellyfish), nobuild`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.7.0`
 - channels(root): `"nixpkgs"`
 - channels(leon): `"home-manager, nixpkgs"`
 - nixpkgs: `/home/leon/.nix-defexpr/channels/nixpkgs`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions