How to move the system setup to flake

Hi,

I understand that this question was asked many times before, but I do not seem to find any discussion that talks about my exact problem. Please, correct me if I am wrong.

I have been trying for a whole day to solve the problem, and I don’t know at this point what is the issue.
I have been following https://www.youtube.com/watch?v=rEovNpg7J0M trying to put my perfectly-working system configuration into flake. However, I get the following error:

$ sudo nixos-rebuild switch --flake /home/user/.config/nixos/#myNixos

warning: creating lock file '/home/user/.config/nixos/flake.lock'
warning: Git tree '/home/user/.config/nixos' is dirty
error:
       … while calling the 'seq' builtin

         at /nix/store/gzv0nsm8d5js3wzq2mr1sv9lfp1iyail-source/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       … while evaluating a branch condition

         at /nix/store/gzv0nsm8d5js3wzq2mr1sv9lfp1iyail-source/lib/modules.nix:261:9:

          260|       checkUnmatched =
          261|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |         ^
          262|           let

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: getting status of '/nix/store/apjwhqxbqag3argvznmp8fl46rirv7wc-source/configuration.nix': No such file or directory

Does anybody know what might be causing this issue? I copied all my config into the folder with the flake, and no matter what I do, this is what I get.
I git committed my flake.nix. I have flake.lock generated, and it looks like this:

{
  "nodes": {
    "nixpkgs": {
      "locked": {
        "lastModified": 1708807242,
        "narHash": "sha256-sRTRkhMD4delO/hPxxi+XwLqPn8BuUq6nnj4JqLwOu0=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "73de017ef2d18a04ac4bfd0c02650007ccb31c2a",
        "type": "github"
      },
      "original": {
        "owner": "NixOS",
        "ref": "nixos-unstable",
        "repo": "nixpkgs",
        "type": "github"
      }
    },
    "root": {
      "inputs": {
        "nixpkgs": "nixpkgs"
      }
    }
  },
  "root": "root",
  "version": 7
}

I have removed some of my previous generations and then did garbage collecting, someone mentioned on the forums that that might be the issue…
I never was able to move the system to the flake, at all.

Thank you for your help!

Hey, such issues are sometimes hard to track down sadly. But it might be helpful to actually see the flake you are trying to use.

Edit: I should also say that flakes are still experimental in Nix. And the issue when and how to remove this experimental status has not been solved. So it is quite okay to not run your system from a flake. Even though I do, too.

Make sur your config is under git and all the files staged correctly.

Hi everyone,

Thank you so much for your replies!
Here is the flake.nix file:

{
  description = "A very basic system setup";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { self, nixpkgs }:
  let
    system = "x86_64-linux";
    pkgs = import nixpkgs {
          inherit system;

          config ={
            allowUnfree = true;
          };
        };
  in
  {
    nixosConfigurations = {
      myNixos = nixpkgs.lib.nixosSystem {
        specialArgs = { inherit system; };
        modules = [
          ./nixos/configuration.nix
        ];
      };
  };
  #packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
  #packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
  };
}

Both my flake.nix and the entire nixos directory are under git version control, both with only a single commit. After I copied my configuration from /etc/nixos, I removed its git commits. Keeping the configuration file under its own git commit is what I was previously doing, and still getting this error.
The file structure is:

./flake.nix
./flake.lock
./nixos/configuration.nix
./nixos/hardware-configuration.nix
./nixos/configuration.nix.bak
./nixos/vim.nix

All of this is in /home/user/.config/nix folder (altered the location since my previous message, instead of nixos as folder in .config, it is nix now, if that matters).
I don’t need vim.nix, it just was copied with other files.

From looking at the contents of the folder that the error asks, flake.nix and its lock file are created, but not the nixos directory.
Another thing that might be relevant is that part of the error I get is the warning “Git tree /home/user is dirty”, since it contains my home configuration. Files under this (~/.config/nix) folder are under their own git version control, so not sure why that warning appears.

In case that is important, here is a full trace of the error:

warning: Git tree '/home/user' is dirty
error:
       … while calling the 'seq' builtin

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       … while evaluating a branch condition

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:261:9:

          260|       checkUnmatched =
          261|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |         ^
          262|           let

       … in the left operand of the AND (&&) operator

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:261:72:

          260|       checkUnmatched =
          261|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |                                                                        ^
          262|           let

       … in the left operand of the AND (&&) operator

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:261:33:

          260|       checkUnmatched =
          261|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |                                 ^
          262|           let

       … while evaluating a branch condition

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:254:12:

          253|
          254|         in if declaredConfig._module.freeformType == null then declaredConfig
             |            ^
          255|           # Because all definitions that had an associated option ended in

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:242:28:

          241|           # For definitions that have an associated option
          242|           declaredConfig = mapAttrsRecursiveCond (v: ! isOption v) (_: v: v.value) options;
             |                            ^
          243|

       … while calling 'mapAttrsRecursiveCond'

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/attrsets.nix:734:5:

          733|     # Attribute set to recursively map over.
          734|     set:
             |     ^
          735|     let

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:234:33:

          233|           ({ inherit lib options config specialArgs; } // specialArgs);
          234|         in mergeModules prefix (reverseList collected);
             |                                 ^
          235|

       … while calling 'reverseList'

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/lists.nix:510:17:

          509|   */
          510|   reverseList = xs:
             |                 ^
          511|     let l = length xs; in genList (n: elemAt xs (l - n - 1)) l;

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:229:25:

          228|       merged =
          229|         let collected = collectModules
             |                         ^
          230|           class

       … while calling anonymous lambda

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:445:37:

          444|
          445|     in modulesPath: initialModules: args:
             |                                     ^
          446|       filterModules modulesPath (collectStructuredModules unknownModule "" initialModules args);

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:446:7:

          445|     in modulesPath: initialModules: args:
          446|       filterModules modulesPath (collectStructuredModules unknownModule "" initialModules args);
             |       ^
          447|

       … while calling 'filterModules'

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:413:36:

          412|       # modules recursively. It returns the final list of unique-by-key modules
          413|       filterModules = modulesPath: { disabled, modules }:
             |                                    ^
          414|         let

       … while calling anonymous lambda

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:439:31:

          438|           disabledKeys = concatMap ({ file, disabled }: map (moduleKey file) disabled) disabled;
          439|           keyFilter = filter (attrs: ! elem attrs.key disabledKeys);
             |                               ^
          440|         in map (attrs: attrs.module) (builtins.genericClosure {

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:400:22:

          399|           let
          400|             module = checkModule (loadModule args parentFile "${parentKey}:anon-${toString n}" x);
             |                      ^
          401|             collectedImports = collectStructuredModules module._file module.key module.imports args;

       … while calling anonymous lambda

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:359:11:

          358|         then
          359|           m:
             |           ^
          360|             if m._class != null -> m._class == class

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:400:35:

          399|           let
          400|             module = checkModule (loadModule args parentFile "${parentKey}:anon-${toString n}" x);
             |                                   ^
          401|             collectedImports = collectStructuredModules module._file module.key module.imports args;

       … while calling 'loadModule'

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:336:53:

          335|       # Like unifyModuleSyntax, but also imports paths and calls functions if necessary
          336|       loadModule = args: fallbackFile: fallbackKey: m:
             |                                                     ^
          337|         if isFunction m then

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:354:14:

          353|           throw "Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}"
          354|         else unifyModuleSyntax (toString m) (toString m) (applyModuleArgsIfFunction (toString m) (import m) args);
             |              ^
          355|

       … while calling 'unifyModuleSyntax'

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:454:34:

          453|      of ‘options’, ‘config’ and ‘imports’ attributes. */
          454|   unifyModuleSyntax = file: key: m:
             |                                  ^
          455|     let

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:354:59:

          353|           throw "Module imports can't be nested lists. Perhaps you meant to remove one level of lists? Definitions: ${showDefs defs}"
          354|         else unifyModuleSyntax (toString m) (toString m) (applyModuleArgsIfFunction (toString m) (import m) args);
             |                                                           ^
          355|

       … while calling 'applyModuleArgsIfFunction'

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:488:39:

          487|
          488|   applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }:
             |                                       ^
          489|     if isFunction f then applyModuleArgs key f args else f;

       … from call site

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/modules.nix:489:8:

          488|   applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }:
          489|     if isFunction f then applyModuleArgs key f args else f;
             |        ^
          490|

       … while calling 'isFunction'

         at /nix/store/1gpgihsi21bni03hy6i5ccw9lfrhnbqi-source/lib/trivial.nix:458:16:

          457|   */
          458|   isFunction = f: builtins.isFunction f ||
             |                ^
          459|     (f ? __functor && isFunction (f.__functor f));

       error: getting status of '/nix/store/92qf7qfqijg9yka9hhh2wwh97v5f16x2-source/.config/nix/nixos': No such file or directory

Thank you again for your help!

This line is not right and likely the cause of problems, try:

-specialArgs = { inherit system; };
+inherit system;

(well, to be more specific, it’s very likely not what you wanted.)

That solve the issue, thank you!