Help migrating stuff to flakes

Hey, been using nixos for a bit, still very much a noob. I have recently decided to start using Flakes and I am currently struggling to get a setup working. Currently the build is struggling with my configuration.nix shenanigans, I had set things up so I could install some unstable branch pkgs on my stable release. As far as I can tell I have set things up properly, however it is not able to identify my unstable packages:

error:
       … while calling the 'head' builtin

         at /nix/store/z3vrsz16ypv60m2pp0gi7x7gpx40gfh1-source/lib/attrsets.nix:1575:11:

         1574|         || pred here (elemAt values 1) (head values) then
         1575|           head values
             |           ^
         1576|         else

       … while evaluating the attribute 'value'

         at /nix/store/z3vrsz16ypv60m2pp0gi7x7gpx40gfh1-source/lib/modules.nix:809:9:

          808|     in warnDeprecation opt //
          809|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          810|         inherit (res.defsFinal') highestPrio;

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

       error: undefined variable 'unstable'

       at /nix/store/5y1ah0npy5fqd5smzky95k11z7mz9pha-source/configuration.nix:209:3:

          208|   pkgs.goverlay
          209|   unstable.freetube
             |   ^
          210|   pkgs.localsend

Here is my flake.nix:

{
  description = "A New Nix Flake that defines my system for the most part now";

  inputs = {
    # NixOS official package source, using the nixos-24.05 branch here
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
    nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs = { self, nixpkgs, nixpkgs-unstable, ... }:
    let
      system = "x86_64-linux";
      lib = nixpkgs.lib;
      pkgs = nixpkgs.legacyPackages.${system};
      pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
      username = "twilight";
      name = "NA";
      unstable = import "${nixpkgs-unstable}" { inherit system; config.allowUnfree = true; };
    in {
      nixosConfigurations = {
        twilight-desktop-nixos = lib.nixosSystem {
        inherit system;
        modules = [
          ./configuration.nix
        ];
        specialArgs = {
          inherit username;
          inherit name;
          inherit pkgs-unstable;
        };
      };
    };
  };
}

and here is my mess of a configuration.nix:

# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

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

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  # Bootloader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  boot.initrd.luks.devices."luks-623032a5-4659-460f-a7de-c25637c6fb61".device = "/dev/disk/by-uuid/623032a5-4659-460f-a7de-c25637c6fb61";
  networking.hostName = "twilight-desktop-nixos"; # Define your hostname.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  #Linux Kernel Version
  boot.kernelPackages = pkgs.linuxPackages_latest;              #If you change this do not forget to change the nct6687d entry in system packages as well
  boot.extraModulePackages = with config.boot.kernelPackages; [ nct6687d ];
  #pkgs.linuxPackages_xanmod_stable;
  #pkgs.linuxPackages_zen;

  boot.kernelModules = [ "i915" "i915-sriov-dkms" "i915-sriov" "nct6687d" "nct" ];

  boot.kernelParams = [
    "i915.enable_guc=3"
    "i915.max_vfs=7"
    "CONFIG_PERF_EVENTS=true"
    "i915.enable_dc=0"
    "i915.verbose_state_checks=true"
    "i915.error_capture=true"
    "acpi_enforce_resources=lax"
  ];

  #boot.supportedFilesystems = [ "zfs" ];
  #boot.zfs.forceImportRoot = false;
  #networking.hostId = "a748301b";

  services.fwupd.enable = true;
  services.envfs.enable = true;
  services.udev.packages = [ pkgs.headsetcontrol ];
  programs.nix-ld.enable = true;
  programs.gamemode.enable = true;
  programs.virt-manager.enable = true;
  programs.adb.enable = true;

  #Change to true once Intel oneapi basekit is in main repo!!!!!!!!!!!
  nix.settings.sandbox = true;
  nix.settings.experimental-features = [ "nix-command" "flakes" ];

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # Enable networking
  networking.networkmanager.enable = true;

  # Set your time zone.
  time.timeZone = "America/Los_Angeles";

  # Select internationalisation properties.
  i18n.defaultLocale = "en_US.UTF-8";

  i18n.extraLocaleSettings = {
    LC_ADDRESS = "en_US.UTF-8";
    LC_IDENTIFICATION = "en_US.UTF-8";
    LC_MEASUREMENT = "en_US.UTF-8";
    LC_MONETARY = "en_US.UTF-8";
    LC_NAME = "en_US.UTF-8";
    LC_NUMERIC = "en_US.UTF-8";
    LC_PAPER = "en_US.UTF-8";
    LC_TELEPHONE = "en_US.UTF-8";
    LC_TIME = "en_US.UTF-8";
  };

  # Enable the X11 windowing system.
  services.xserver.enable = true;

  # Enable the KDE Plasma Desktop Environment.
  services.displayManager.sddm.enable = true;
  services.xserver.desktopManager.plasma5.enable = true;
  services.displayManager.defaultSession = "plasmawayland";
  services.displayManager.sddm.wayland.enable = true;

  #Bluetooth
  hardware.bluetooth.enable = true;
  hardware.bluetooth.powerOnBoot = true;

  # Configure keymap in X11
  services.xserver = {
    xkb = {
      layout = "us";
      variant = "";
    };
  };

  virtualisation.containers.enable = true;
    virtualisation = {
      podman = {
        enable = true;

        # Create a `docker` alias for podman, to use it as a drop-in replacement
        dockerCompat = true;
  
        # Required for containers under podman-compose to be able to talk to each other.
        defaultNetwork.settings.dns_enabled = true;
    };
#      virtualbox.host = {
#       enable = true;
#       enableExtensionPack = true;
#      };
      waydroid.enable = true;
#      vmware.host.enable = true;
      spiceUSBRedirection.enable = true;
      libvirtd = {
        enable = true;
          qemu = {
           package = pkgs.qemu_kvm;
           runAsRoot = true;
           swtpm.enable = true;
           ovmf = {
             enable = true;
             #packages = [(pkgs.unstable.OVMF.override {
               #secureBoot = true;
               #tpmSupport = true;
             #}).fd];
           };
        };
     };
  };

  services.flatpak.enable = true;

  # Enable CUPS to print documents.
  services.printing.enable = true;

  # Enable sound with pipewire.
  sound.enable = true;
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    # If you want to use JACK applications, uncomment this
    jack.enable = true;

    # use the example session manager (no others are packaged yet so this is enabled by default,
    # no need to redefine it in your config for now)
    #media-session.enable = true;
  };

  # Enable touchpad support (enabled default in most desktopManager).
  # services.xserver.libinput.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.twilight = {
    isNormalUser = true;
    description = "NA";
    extraGroups = [ "networkmanager" "wheel" "libvirtd" "user-with-access-to-virtualbox" "dialout" "adbusers" "perf_users"  ];
    packages = with pkgs; [
      #firefox
      kate
    #  thunderbird
    ];
  };

#  nixpkgs = {
#    config = {
#      allowUnfree = true;
#      packageOverrides = pkgs: {
#        unstable = import <nixos-unstable> {
#          config = config.nixpkgs.config;
#        };
        #prev = import <nixos-prev> {
        #  config = config.nixpkgs.config;
        #};
#        nur = import (builtins.fetchTarball "https://github.com/nix-community/NUR/archive/master.tar.gz") {
#          inherit pkgs;
#        };
#      };
#    };
#    overlays = [
#    ];
#  };

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment = {
    systemPackages = with pkgs; [
  vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
  wget
  wineWowPackages.stable
  #pkgs.nixVersions.latest
  pkgs.steam
  pkgs.mangohud
  pkgs.gamescope
  pkgs.lutris
  pkgs.ungoogled-chromium
  pkgs.prismlauncher
  pkgs.jdk
  pkgs.armcord
  pkgs.goverlay
  unstable.freetube
  pkgs.localsend
  unstable.xivlauncher
  unstable.input-remapper               #Flip back to Unstable at some point
  pkgs.flatpak
  pkgs.pciutils
  pkgs.clinfo
  pkgs.mesa-demos
  pkgs.vulkan-tools
  pkgs.wayland-utils
  unstable.bluez
  unstable.bluez-alsa
  unstable.bluez-tools
  pkgs.killall
  pkgs.git
  pkgs.cmake
  pkgs.gnumake
  pkgs.gcc
  pkgs.winetricks
  pkgs.protontricks
  pkgs.opensycl
  pkgs.sycl-info
  pkgs.ncurses
  dive            # look into docker image layers
  podman-tui      # status of containers in the terminal
  #docker-compose # start group of containers for dev
  podman-compose  # start group of containers for dev
  pkgs.distrobox
  pkgs.spice
  pkgs.spice-gtk
  pkgs.curl
  pkgs.spotify
  pkgs.headsetcontrol
  pkgs.headset-charge-indicator
  #nur.repos.gricad.intel-oneapi
  #nur.repos.xddxdd.i915-sriov
  pkgs.python3
  pkgs.lua54Packages.lua
  pkgs.lua54Packages.luasocket
  pkgs.lua54Packages.luasec
  pkgs.virglrenderer
  pkgs.icu
  pkgs.libdecor
  pkgs.SDL2
  pkgs.faudio
  pkgs.libgcrypt
  pkgs.unzip
  pkgs.libunwind
  pkgs.davinci-resolve
  pkgs.tio
  pkgs.gptfdisk
  #pkgs.partition-manager
  pkgs.gparted
  pkgs.ventoy-full
  pkgs.mlv-app
  pkgs.darling
  pkgs.darling-dmg
  pkgs.ocl-icd
  pkgs.zluda
  pkgs.openvino
  #pkgs.python311Packages.openvino
  #pkgs.python311Packages.setuptools
  pkgs.libreoffice-qt
  #pkgs.conda
  pkgs.libgcc.lib
  #pkgs.obs-studio
  #pkgs.obs-studio-plugins.obs-vkcapture
  pkgs.ffmpeg_5-full
  libsForQt5.kdeconnect-kde
  pkgs.lm_sensors
  pkgs.fuse
  pkgs.intel-gpu-tools
  pkgs.intel-graphics-compiler
  pkgs.graalvm-ce
  pkgs.libdvdcss
  pkgs.libdvdread
  #pkgs.handbrake
  pkgs.intel-media-sdk
  pkgs.dvdbackup
  pkgs.steamtinkerlaunch
  pkgs.android-studio
  pkgs.p7zip
  pkgs.llama-cpp
  pkgs.openjdk8-bootstrap
  pkgs.kdePackages.filelight
  pkgs.godot_4
  pkgs.godot_4-export-templates
  pkgs.gdtoolkit
  pkgs.pixelorama
  pkgs.vkbasalt-cli
  pkgs.libunwind
  pkgs.clang
  pkgs.pkg-config
  pkgs.keybinder3
  ];
  };

  programs = {
    steam = {
      enable = true;
      remotePlay.openFirewall = true;
      dedicatedServer.openFirewall = true;
      gamescopeSession.enable = true;
      package = pkgs.steam.override {
        extraPkgs = pkgs:
          with pkgs; [
            xorg.libXcursor
            xorg.libXi
            xorg.libXinerama
            xorg.libXScrnSaver
            libpng
            libpulseaudio
            libvorbis
            stdenv.cc.cc.lib
            libkrb5
            keyutils
            steamtinkerlaunch
          ];
      };
    };
    coolercontrol.enable = true;
#    xivlauncher.package = pkgs.xivlauncher.override {
#      extraPkgs = pkgs:
#        with pkgs; [
#          prev.mesa
#          prev.driversi686Linux.mesa
#        ];
#    };
#    java = {
#      enable = true;
#      package = pkgs.graalvm-ce;
#    };
  };

  networking.firewall = {
    enable = true;
    allowedTCPPorts = [ 53317 42000 42001 ];
    allowedUDPPorts = [ 53317 42000 42001 ];
    allowedTCPPortRanges = [
      {
        from = 1714;
        to = 1764;
      }
    ];
    allowedUDPPortRanges = [
      {
        from = 1714;
        to = 1764;
      }
    ];
  };

  hardware.opengl = with pkgs; {
    enable = true;
    driSupport32Bit = true;
    package = pkgs.mesa.drivers;
    package32 = pkgs.driversi686Linux.mesa.drivers;
    extraPackages = with pkgs; [
      unstable.intel-compute-runtime
      unstable.level-zero
      unstable.intel-gmmlib
      intel-ocl
      intel-media-driver
      rocmPackages.rocm-runtime
      rocmPackages.clr.icd
      #rocmPackages.clr
      pkgs.libdrm
    ];
  };

  hardware.intel-gpu-tools.enable = true;

  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  # services.openssh.enable = true;

  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "23.11"; # Did you read the comment?

}

Any help would be great ty!

Hey. The error is a simple “undefined variable” error. That unstable variable does not exist at that scope.

By the way, I think most NixOS desktop users go for nixpkgs-unstable. I know I do.

Hmm, I geuss I thought I had defined it and that is my confusion. Maybe I’ll go unstable at some point but at the moment I do not want to. Might I ask why it is not defined? and where did I get lost in trying to writing my flake?

Seems like it’s defined in a different file.

Okay, I am probably not understanding the documention I can find well enough to figure it out on my own. Would you be willing to give me an example of what I need to do and explain to me how it works please.

To make an arbitrary value available in the arguments of the modules, you’d probably use specialArgs. Here’s an example from my configs:

You can find many more example on GitHub.

I recommend learning about the language and the module system at https://nix.dev.