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

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";
}

3 Likes