DEV Community

Ray Harris
Ray Harris

Posted on • Updated on

Day 2: Introducing Configuration.nix

My todos from Day 1 were as follows:

  • Get Wi-Fi working
  • Get Macbook keyboard, touch bar, and trackpad working
  • Learn how to use the OS

The OS config is supposed to be configured entirely with functional expressions, so priority #1 became learning to manage the configuration. Turns out, there's a nifty command for doing just that:

sudo nixos-rebuild edit
Enter fullscreen mode Exit fullscreen mode

This will open the configuration.nix file in Nano, a text editor for the terminal. This is probably uncomfortable for anyone new to linux, but it works great. Forget the mouse, use the arrow keys, pageup/pagedown, CTRL+O to save and CTRL+X to exit. Easy peasy.

Now, reading the code in here is fairly straightforward. Understanding it is another level of technicality. Here is a perfect example:

  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.
  # Enable networking
  networking.networkmanager.enable = true;
Enter fullscreen mode Exit fullscreen mode

These two lines control how the operating system manages the network interfaces. What you might not know if you don't have prior linux experience is that NetworkManager and wpa_supplicant do not play nice without being properly introduced to each other. Simply uncommenting will not work.

So I simply uncommented and saved the file. đź’€

The configuration.nix file is updated! What next? Next we need to rebuild NixOS using the updated file. We'll also need to switch to the new build for the changes to take effect. A keyword to learn here is "Generation". When you rebuild, you create a new generation. Here's the command:

sudo nixos rebuild switch
Enter fullscreen mode Exit fullscreen mode

And this is where the beauty of NixOS rears its ugly head. There's some kool-aid available here, but basically if you mess up, NixOS can't build, and if it can't build, your mistake can't break your system. And if you have another mistake that can "break" something, you can always roll back to a previous "generation". Since every generation is defined so clearly, you can know that they basically all work.

So when I ran my first nixos rebuild switch command, I got an error (remember the unacquainted network interface managers). I haven't resolved this yet, but there's a section in the manual to help figure it out. For now, I've reverted my change and kept the wireless line commented out. I'm still using a wired connection as of writing this :)

A little struggle, but that's Okay. One thing I need for sure is to be able to modify my configuration.nix file more comfortably. One great thing about blogging as a noob for noobs is that nobody is going to comment about using vim or emacs to be superior. Nope. My people are using Sublime and VSCode. And I want to install VSCode.

  1. Run echo $EDITOR to see what you have as your default editor now
  2. Find the name of the vscode package by searching https://search.nixos.org/packages
  3. Add vscode to your configuration.nix near the bottom. Here's what my file ended up looking like:
  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
  vscode
  # vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
  #  wget
  ];
Enter fullscreen mode Exit fullscreen mode
  1. I also added a line at above this section:
  environment.variables.EDITOR = "code";
Enter fullscreen mode Exit fullscreen mode
  1. By the way, if you didn't allow unfree when installing your OS, you can do that now by making sure this line is in your configuration and set to true
  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;
Enter fullscreen mode Exit fullscreen mode

I wish the executable name were "vscode" instead of "code" but Microsoft gets away with a lot.

Now after saving my changes to configuration.nix, I get to try running sudo nixos rebuild switch again. And good news, it worked! I can now open vscode, open my etc/nixos/configuration/nix file, and make changes in a nice friendly UI.

If only we had some syntax highlighting. How do we add vscode extensions in NixOS? Turns out, we just need to install a different package in place of vscode in the environment.systemPackages. Like this:

    (vscode-with-extensions.override {
      vscodeExtensions = with vscode-extensions; [
        bbenoist.nix
      ];
    })
Enter fullscreen mode Exit fullscreen mode

It's kind of complex and I haven't fully internalized the syntax and the meaning of it all, but after some googling I found that my config should look like this:

  environment.systemPackages = with pkgs; [
    (vscode-with-extensions.override {
      vscodeExtensions = with vscode-extensions; [
        bbenoist.nix
      ];
    })
  # vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
  #  wget
  ];
Enter fullscreen mode Exit fullscreen mode

The nix syntax highlighting is available thanks to a French dude's extension called bbenoist.nix which you can see on github here. Thanks Baptist!

That's enough for today! The more I learn, the less I know. Future topics:

  • Getting my Macbook hardware to work (keyboard, mouse, speakers, camera, wifi, everything)
  • Better understanding the nix language and configuration expressions
  • Improving my desktop experience. The default app-switcher in KDE/Plasma is gross.

Top comments (0)