I can't nixos-rebuild switch due to error in flake.nix at 1:1

so whenever I switch, it just says:

~ > sudo nixos-rebuild switch --flake /etc/nixos#luNix
error: syntax error, unexpected end of file
       at /nix/store/pip5wzdpzc9jkyjd5gl4mgpxmkwv7f48-source/flake.nix:1:1:
Command 'nix --extra-experimental-features 'nix-command flakes' build --print-out-paths '/etc/nixos#nixosConfigurations."luNix".config.system.build.nixos-rebuild' --no-link' returned non-zero exit status 1.

and here are the files i have in /etc/nixos

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, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).

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

#{ nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
#  "google-chrome"
#  ];
#}


# { config, pkgs, ... }:



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

  nixpkgs.config.allowUnfree = true;
  programs.fish.enable = true;
  # Use the systemd-boot EFI boot loader.
  boot.loader.grub.device = "nodev";
  boot.loader.grub.efiSupport = true;
  boot.loader.grub.useOSProber = true;
  boot.loader.efi.canTouchEfiVariables = true;

  # Use latest kernel.
  boot.kernelPackages = pkgs.linuxPackages_latest;

  networking.hostName = "luNix"; # Define your hostname.

  # Configure network connections interactively with nmcli or nmtui.
  networking.networkmanager.enable = true;

  # Set your time zone.
  time.timeZone = "Asia/Ho_Chi_Minh";
  services.timesyncd.enable = true;
  networking.timeServers = [ "pool.ntp.org" ];
  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";


  # Select internationalisation properties.
  i18n.defaultLocale = "en_US.UTF-8";
  console = {
    font = "Lat2-Terminus16";
    keyMap = "us";
    # useXkbConfig = true; # use xkb.options in tty.
  };

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

  # Configure keymap in X11
  services.xserver.xkb.layout = "us";
  # services.xserver.xkb.options = "eurosign:e,caps:escape";

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

  # Enable sound.
  # services.pulseaudio.enable = true;
  # OR
  services.pipewire = {
    enable = true;
    pulse.enable = true;
    alsa.enable = true;
    wireplumber.enable = true;
  };
  #services.pipewire.wireplumber.enable = true;
  
  # Enable touchpad support (enabled default in most desktopManager).
  services.libinput.enable = true;

  #Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.knam = {
    isNormalUser = true;
    extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
    packages = with pkgs; [
      tree
    ];
    shell = pkgs.fish;
  };

  # programs.firefox.enable = true;

  # List packages installed in system profile.
  # You can use https://search.nixos.org/ to find more packages (and options).
  environment.systemPackages = with pkgs; [
     # xfce4-goodies
     nano # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
     wget
     google-chrome
     lightdm
     lightdm-gtk-greeter
     fish
     fastfetch
     kitty
     redshift
     git
     #xfce4-goodies
     xfce4-genmon-plugin
     xfce4-whiskermenu-plugin
     xfce4-pulseaudio-plugin
     sassc
     ntfs3g
     p7zip
     hyprland
     xdg-desktop-portal-hyprland
     nautilus
     nwg-look
     #qt6Packages.fcitx5-configtool
     #qt6Packages.fcitx5-unikey
     qt6Packages.qt6ct
     qt6Packages.qtstyleplugin-kvantum
     wl-clipboard
     wl-clip-persist
     grim
     slurp
     gnome-characters
     gnomeExtensions.dash-to-dock
     gnomeExtensions.runcat
     gnomeExtensions.compiz-windows-effect
     gnome-extension-manager
     linuxKernel.packages.linux_7_1.cpupower
     waybar
     hyprsunset
     brightnessctl
     awww
     # wine
     # wine64
     wine-wayland
     vmware-workstation
     gamemode
     wineWow64Packages.stable
     mesa
     mesa-demos
     vulkan-tools
     vulkan-loader
     gpu-screen-recorder
     gpu-screen-recorder-gtk
     libva
     libva-utils
     intel-media-driver
     zenity
     polkit
     polkit_gnome
     jdk25
     # musescore
     nix-ld
     jq
     # obs-studio
     xorg.libXxf86vm
     xinit
     maim
     picom-pijulius
     polybarFull
     polybar-pulseaudio-control
     bspwm
     sxhkd
     feh
     xrandr
     libxcvt
     slop
     btop
     glava
     gnome-tweaks
     pciutils
     # nvidia-smi
     # nvidia-utils
     # applicatory
  ];
  services.xserver.videoDrivers = [ "nvidia" ];

  # idk
  nix.settings.experimental-features = [ "nix-command" "flakes" ];
  programs.steam.protontricks.enable = true;
  programs.gamemode.enable = true;
  programs.appimage.enable = true;
  programs.nix-ld.enable = false;
  programs.nix-ld.libraries = with pkgs; [
    stdenv.cc.cc
    zlib
    openssl
    libx11
    libxext
    libxrender
    libxcb
    libxkbcommon
    glib
    gtk3
    libGL
    mesa
    libglvnd
    libpulseaudio
    libx11
    libxext
    xorg.libXrender
    xorg.libxcb
    xorg.libXrandr
    xorg.libXi
    xorg.libXcursor
    xorg.libXdamage
    xorg.libXfixes
    fontconfig
    egl-x11
    egl-wayland
    libxkbcommon
    dbus
    freetype
    zstd
    brotli
    krb5
    pipewire
    wayland
    wayland-protocols
    # btop
  ];
  security.polkit.enable = true;
  systemd.user.services.polkit-gnome-authentication-agent-1 = {
    description = "polkit-gnome-authentication-agent-1";
    wantedBy = [ "graphical-session.target" ];
    wants = [ "graphical-session.target" ];
    after = [ "graphical-session.target" ];

    serviceConfig = {
      Type = "simple";
      ExecStart = "${pkgs.polkit_gnome}/libexec/polkit-gnome-authentication-agent-1";
      Restart = "on-failure";
      RestartSec = 1;
    };
  };

  # vmware
  virtualisation.vmware.host.package = pkgs.vmware-workstation;
  virtualisation.vmware.host.enable = true;
  virtualisation.vmware.guest.enable = true;

  # Kyeboard
  i18n.inputMethod = {
    enable = true;
    type = "fcitx5";

    fcitx5.addons = with pkgs; [
      qt6Packages.fcitx5-unikey
      fcitx5-gtk
      qt6Packages.fcitx5-configtool
    ];
  };

  # gnome
  services.desktopManager.gnome.enable = true;
  programs.seahorse.enable = true;
  services.gnome.gnome-keyring.enable = true;
  services.gnome.core-apps.enable = true;

  services.xserver.windowManager.bspwm.enable = true;

  # Hyprland
  programs.hyprland.portalPackage = pkgs.xdg-desktop-portal-hyprland;
  programs.hyprland.enable = true;
  programs.hyprland.package = pkgs.hyprland;
  services.hypridle.enable = true;
  programs.hyprlock.enable = true;
  programs.hyprland.xwayland.enable = true;

  #font
  fonts.packages = with pkgs; [
     noto-fonts
     noto-fonts-cjk-sans
     noto-fonts-cjk-serif
     noto-fonts-color-emoji
     font-awesome_7
     (pkgs.stdenv.mkDerivation {
       pname = "Mali";
       version = "1.0";
       src = ./Mali-Regular.ttf;
       dontUnpack = true;
       installPhase = ''
         mkdir -p $out/share/fonts/truetype
         cp $src $out/share/fonts/truetype/
       '';
     })
     (pkgs.stdenv.mkDerivation {
       pname = "MapleMono";
       version = "1.0";
       src = ./MapleMono-Regular.ttf;
       dontUnpack = true;
       installPhase = ''
         mkdir -p $out/share/fonts/truetype
         cp $src $out/share/fonts/truetype/
       '';
     })
     nerd-fonts."m+"
  ];

  fonts.fontconfig.localConf = ''
    <?xml version="1.0"?>
    <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
    <fontconfig>

      <match target="pattern">
        <test name="family">
          <string>sans-serif</string>
        </test>
        <edit name="family" mode="prepend" binding="strong">
          <string>Mali</string>
        </edit>
      </match>

      <match target="pattern">
        <test name="family">
          <string>monospace</string>
        </test>
        <edit name="family" mode="prepend" binding="strong">
          <string>Maple Mono</string>
        </edit>
      </match>

    </fontconfig>
  '';

  # ome 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;
  # services.lightdm.enable = true;
  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # Copy the NixOS configuration file and link it from the resulting system
  # (/run/current-system/configuration.nix). This is useful in case you
  # accidentally delete configuration.nix.
  # system.copySystemConfiguration = true;

  # This option defines the first version of NixOS you have installed on this particular machine,
  # and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions.
  #
  # Most users should NEVER change this value after the initial install, for any reason,
  # even if you've upgraded your system to a new NixOS release.
  #
  # This value does NOT affect the Nixpkgs version your packages and OS are pulled from,
  # so changing it will NOT upgrade your system - see https://nixos.org/manual/nixos/stable/#sec-upgrading for how
  # to actually do that.
  #
  # This value being lower than the current NixOS release does NOT mean your system is
  # out of date, out of support, or vulnerable.
  #
  # Do NOT change this value unless you have manually inspected all the changes it would make to your configuration,
  # and migrated your data accordingly.
  #
  # For more information, see `man configuration.nix` or https://nixos.org/manual/nixos/stable/options#opt-system.stateVersion .
  system.stateVersion = "26.05"; # Did you read the comment?

}
hardware-configuration.nix
# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, modulesPath, ... }:

{
  imports =
    [ (modulesPath + "/installer/scan/not-detected.nix")
    ];

  nixpkgs.config.allowUnfree = true;
  nixpkgs.config.nvidia.acceptLicense = true;
  hardware.nvidia = {
    # enabled = true;
    modesetting.enable = true;
    open = false;
    nvidiaSettings = true;
    package = config.boot.kernelPackages.nvidiaPackages.legacy_470;
    prime = {
      offload.enable = true;
      intelBusId = "PCI:0:2:0";
      nvidiaBusId = "PCI:1:0:0";
    };
  };

  hardware.graphics = {
    enable = true;
  };

  boot.initrd.availableKernelModules = [ "xhci_pci" "ehci_pci" "ahci" "usb_storage" "sd_mod" "rtsx_pci_sdmmc" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-intel" ];
  boot.extraModulePackages = [ ];

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/e044875c-b0b1-4867-bc92-8bb5a991c3ad";
      fsType = "ext4";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/5C60-E51E";
      fsType = "vfat";
      options = [ "fmask=0022" "dmask=0022" ];
    };

  swapDevices = [{
    device = "/swapfile";
    size = 8 * 1024;
  }];

  nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
  hardware.cpu.intel.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
}
flake.nix
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs = inputs@{ self, nixpkgs, ... }: {
    nixosConfigurations.luNix = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      specialArgs = {
        inherit inputs;
      };
      modules = [
        ./configuration.nix
      ];
    };
  };
}

Mali-Regular.ttf

MapleMono-Regular.ttf

flakes/

nothing in here

Looks like you might have lost some files to filesystem corruption, most likely due to a power interruption. Try:

sudo nix-store --verify --check-contents

A sudo nix-collect-garbage -d sudo nix-collect-garbage might get rid of the bad paths if they’re luckily unused, which should cause them to be regenerated correctly.

(edit: sudo nix-collect-garbage -d is a bit of a bigger gun and removes old nixos generations. it might not be worth it. see below)

The -d will delete previous generations of your system, so maybe you can try without -d first.

2 Likes

This is also not an amazing way to solve that, just use the --repair flag on the nix store verify to achieve this. It’ll delete paths that are corrupt, and attempt to refetch them.

Collecting garbage beforehand will reduce the number of paths to check a bit, but is in no way a good solution to corrupt store paths.

In this case, I just simply don’t see --repair somehow managing “refetching” this user’s system flake. But yes, if there are paths that can be repaired by downloading a good copy from something like cache.nixos.org, --repair is a good thing.

Personally, I don’t really like advocating to do --repair without seeing the extent of the damage. It’s not a panacea for broken paths. This is as opposed to garbage collection, which just eliminates problems that don’t need fixing while leaving less to look at. (edit this is incorrect, see below)

--repair also deletes corrupted paths, so a rebuild after repair does exactly the same as deleting the corrupted paths on accident with nix-collect-garbage, except it’s guaranteed to work.

Oh, TIL. Thanks for the information

Only if they are not “alive”.

1 Like

Sure, but that’s true of any store path deletion by nix. GC or nix-store --delete only deletes dead paths as well, as complete deployment is a core expectation of nix.

1 Like

I just had a similar problem as of now and doing a filesystem check with

sudo touch /forcefsck

then rebooting was a necessary step to fixing it for me.

1 Like