Search code examples
nixnixos

Populate NixOS VM wiith files


I work with NixOS VMs, I can add packages and services, but I struggle to add files (here some configuration and certificates, /etc, and a directory in a user).

I have tried the following:

{ config, lib, pkgs, ... }:

let
  srcs-etc =
    pkgs.stdenv.mkDerivation
      {
        name = "src-etc";
        src = ./.;
        buildInputs = [ ];
        unpackPhase = "true";
        buildPhase = ''
          mkdir -p "$out/etc/nixos"
          cp "$src/configuration.nix" "$out/etc/nixos"
          cp "$src/nix-serve.pem" "$out/etc/nixos"
          cp "$src/nix-serve.pub" "$out/etc/nixos"
        '';
      };
  srcs-project =
    pkgs.stdenv.mkDerivation
      {
        name = "src-project";
        src = ./project;
        buildInputs = [ ];
        unpackPhase = "true";
        buildPhase = ''
          mkdir -p "$out/project"
          cp -r "$src" "$out/project"
        '';
      };
in
{
  imports = [ ];

  # Use the systemd-boot EFI boot loader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;
  networking = {
    hostName = "ci";
  };
  users.users.runner = {
    isNormalUser = true;
    extraGroups = [ ];
    packages = [ srcs-project ];
  };

  environment.systemPackages = with pkgs; [ srcs-etc ];
  system.stateVersion = "23.11"; # Did you read the comment?
}

However all the here-above directories are still empty.


Solution

  • It's easy to add files to etc, you just need to set the environment.etc options inside the config section of a NixOS module. See: