Creating an sdImage with `nix build nixpkgs#...`

Out of curiousity – I know I can build an sdImage like this:

# nix build \
    --include nixos-config=./sd-image.nix \
    --argstr system aarch64-linux \
    --file '<nixpkgs/nixos>' \
    --show-trace \
    config.system.build.sdImage

– is there any way for me to do the same with flakes “style” from the CLI, without having to write a new flake.nix?

For example nixosSystem looked promising, but it doesn’t seem like there is any way to pass in args.modules:

# nix build --dry-run nixpkgs#lib.nixosSystem
error: flake output attribute 'lib.nixosSystem' is not a derivation

I was also trying a number of things like nixpkgs#nixosModules, or seeing that nixpkgs#legacyPackages.aarch64-linux.config exists:

# nix repl
nix-repl> :lf nixpkgs
nix-repl> :p legacyPackages.aarch64-linux.config
{ allowAliases = true; allowBroken = false; allowUnfree = false; allowUnsupportedSystem = false; contentAddressedByDefault = false; doCheckByDefault = false; enableParallelBuildingByDefault = false; showDerivationWarnings = [ ]; strictDepsByDefault = false; warnings = [ ]; }

and noting that config seemed like the base of config.system.build.sdImage, I tried:

# nix build --dry-run \
    nixpkgs#legacyPackages.aarch64-linux.config.system.build.sdImage
error: flake 'flake:nixpkgs' does not provide attribute 'packages.aarch64-linux.legacyPackages.aarch64-linux.config.system.build.sdImage', 'legacyPackages.aarch64-linux.legacyPackages.aarch64-linux.config.system.build.sdImage' or 'legacyPackages.aarch64-linux.config.system.build.sdImage'

Issues about not being able to pass args to flakes (which I think is what’s missing – specifically args.modules, right?):

This might help: GitHub - gytis-ivaskevicius/orangepi-r1-plus-nixos-image at local-changes-needs-double-check

Well I’m pretty sure this is impossible.

I think the below should work, but the error is pretty clear. Something similar seems to work in nix repl.

$ nix build nixpkgs#nixos.lib.nixosSystem --arg args '{ system = "aarch64-linux"; modules = []; }'
error: '--arg' and '--argstr' are incompatible with flakes