Nixops2 deploy flakes

Since the tip from my earlier question:
https://discourse.nixos.org/t/different-channel-generation/17628/6
I’ve decided to migrate to using flakes, and added experimental-features = nix-command flakes to /etc/nix/nix.conf as instructed.

Next, I’ve figured out how to install nixops2:
nix-env -i nixops-with-plugins will install v2.0.0 (at time of writing)
nix-env -i nixops will install v1.7 (at time of writing)

edit: Nixops2 will revert nix from v2.6.0 to v2.4 on install, so I ran nix upgrade-nix to get back to v2.6.0.

Now, my current nixops.nix with the added network.storage.legacy = {}; gives the error: Exception: could not find specified deployment in state file ‘/tmp/tmp88u3n4lknixops/state.nixops’ I’m not sure why it would look in a tmp directory? Do I perhaps have to explicitly provide the location of the default legacy SQLite database?
Simply chaning it to network.storage.memory = {}; seems to work without error.

Unfortunately I run into another error when using flake.nix:

nixops deploy
error: flake 'path:/workspaces/deployments/local-vms' requires lock file changes but they're not allowed due to '--no-update-lock-file'

nixops deploy --recreate-lock-file
usage: nixops [-h] [--version] [--pdb] operation ...
nixops: error: unrecognized arguments: --recreate-lock-file

How to proceed?

Ok, from this issue I figured to try running nix flake lock & nix flake update which resolves the problem above :slight_smile:

And with a lot of trial and error to resolve this error: https://github.com/NixOS/nixops/blob/ce1c57ec104636954296c229468bb65fa4ec7f8d/nix/eval-machine-info.nix#L45
I figured I could try

inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-21.11";
outputs = { self, nixpkgs, ... }: {
        nixopsConfigurations.default = {
                inherit nixpkgs;
        };
};

Which outputs:

WARNING: NixOps 1.0 -> 2.0 conversion step required

NixOps 2.0 added support for multiple storage backends.

Upgrade steps:
1. Open /workspaces/deployments/local-vms
2. Add:
    network.storage.legacy = {
      databasefile = "~/.nixops/deployments.nixops";
    };
3. Rerun

See https://nixops.readthedocs.io/en/latest/manual/migrating.html#state-location for more guidance.

which relates to the question above about the location of the nixops state.

When I then add:

network.storage.legacy = {
    databasefile = "~/.nixops/deployments.nixops";
};

I get the same error as before:

Exception: could not find specified deployment in state file ‘/tmp/tmps5ws3p5znixops/state.nixops’

So I feel like I’m making progress, but also that I’m a bit stuck.

I suspect the issue could be related to the ~/.nixops/... path being a bit indeterminate?

Note that it’s possible to export the state to json. I think roughly something like

nixops export -d my-deployment > legacy-state.json

To import to a relative ./state.nixops, use NIXOPS_STATE

NIXOPS_STATE=$(pwd)/state.nixops nixops import -d my-new-deployment < legacy-state.json
NIXOPS_STATE=$(pwd)/state.nixops nixops info -d my-new-deployment

Not sure if thats what you would want, but thought it might be helpful to be aware of export/import if you aren’t