Nixos-rebuild: How to specify configuration.nix when attempting to build/deploy remotely

I’m hoping someone can assist with either correcting a misunderstanding on my part or perhaps, see something I missed and point me in the right direction.

A bit of context:

LocalHost: NixOS 23.05 (x86_64-linux), installed using a flake (has no /etc/configuration.nix)

RemoteHost: Raspberry Pi 4 (aarch64-linux), only freshly booted from a recent 23.05 hydra installer image, slightly modified with authorized_keys for headless setup

My goal was to use nixos-rebuild from my LocalHost machine, using a configuration.nix file as documented here and specifying both the build-host and target-host as user@remote_ip

It seems no matter what I attempt, I always end up copying some 6700 drvs from the LocalHost to the RemoteHost, rather than telling the RemoteHost to build itself from the configuration.nix file I’m attempting to supply to it.

I’ve tried:
nixos-rebuild -I nixos-config=$(pwd)/configuration.nix --build-host user@remote_ip --target-host user@remote_ip --fast --use-remote-sudo switch

This ends up failing after copying a bunch of stuff from my LocalHost /nix/store saying nix-store -I argument unknown (or something to that effect)

I’ve also tried:
(export NIX_PATH=nixos-config=$(pwd)/configuration.nix ; nixos-rebuild --build-host user@remote_ip --target-host user@remote_ip --fast --use-remote-sudo switch)

This just fails because it tries to build a drv that says requires “x86_64-linux with features {}”, as opposed to the “aarch64-linux with features…”

Ideally, my goal here was to fresh boot my raspberry pi(s), “initialize” them using a basic configuration.nix, basically enabling flake support, and then rebuilding using the appropriate flake configuration.

Any help would be appreciated; whether that is directly related to my initial goal, or recommending an alternative :smiley:

1 Like

I’ll leave this here for anyone having this particular issue.

Had I turned on verbose mode (-v) when running nixos-rebuild, I would’ve noticed that it went into “flake mode” almost immediately, because I had a flake.nix file sitting in /etc/nixos. The following command

nixos-rebuild -I nixos-config=$(pwd)/configuration.nix --build-host user@remote_ip --target-host user@remote_ip --fast --use-remote-sudo switch -v

produced the following output with an empty flake.nix file present in /etc/nixos

building the system configuration...
Building in flake mode.
$ nix --extra-experimental-features nix-command flakes eval --raw /etc/nixos#nixosConfigurations."SYSTEM_NAME".config.system.build.toplevel.drvPath -I nixos-config=/home/user/Templates/configuration.nix -v

In this example, it immediately failed, since I merely put an empty flake.nix file there. During previous runs it attempted to use my system flake and completely ignored the nixos-config setting that you can see above.

It seems like that may be a bug? I honestly don’t know enough to present it as such.

In any event, the initial command I expected to work (below), did actually work once I temporarily moved my /etc/nixos/flake.nix file somewhere else.

Working command
nixos-rebuild -I nixos-config=$(pwd)/configuration.nix --build-host user@remote_ip --target-host user@remote_ip --fast --use-remote-sudo switch

Example config

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

let
  hostname = "nixos";
  user = "nixos";
  timeZone = "America/New_York";
  defaultLocale = "en_US.UTF-8";
in {
  nix.settings.experimental-features = [ "nix-command" "flakes" ];
  nixpkgs = {
    hostPlatform = "aarch64-linux";
    config = {
      allowUnfree = true;
    };
  };

  boot = {
    kernelPackages = pkgs.linuxKernel.packages.linux_rpi4;
    initrd.availableKernelModules = [ "xhci_pci" "usbhid" "usb_storage" ];
    loader = {
      grub.enable = false;
      generic-extlinux-compatible.enable = true;
    };
  };

  fileSystems = {
    "/" = {
      device = "/dev/disk/by-label/NIXOS_SD";
      fsType = "ext4";
      options = [ "noatime" ];
    };
  };

  networking.hostName = hostname;

  environment.systemPackages = with pkgs; [
  ];

  services.openssh.enable = true;

  time.timeZone = timeZone;

  i18n = {
    defaultLocale = defaultLocale;
    extraLocaleSettings = {
      LC_ADDRESS = defaultLocale;
      LC_IDENTIFICATION = defaultLocale;
      LC_MEASUREMENT = defaultLocale;
      LC_MONETARY = defaultLocale;
      LC_NAME = defaultLocale;
      LC_NUMERIC = defaultLocale;
      LC_PAPER = defaultLocale;
      LC_TELEPHONE = defaultLocale;
      LC_TIME = defaultLocale;
    };
  };

  users = {
    mutableUsers = false;
    users."${user}" = {
      isNormalUser = true;
      hashedPassword = "!";
      extraGroups = [ "wheel" ];

      openssh.authorizedKeys.keys = [
        "ssh-ed25519 ..."
      ];
    };
  };

  security.sudo.extraRules= [
  {
    users = [ user ];
    commands = [
    {
      command = "ALL" ;
      options= [ "NOPASSWD" ];
    }];
  }];

  
  system.stateVersion = "23.05";
}

1 Like

I don’t mean to unnecessarily bump this thread, but I cannot seem to edit either of the posts at this point.

Several days later, I’ve just noticed that the nixos-rebuild command has a --no-flake argument that would’ve resolved my issue as well.

1 Like