tty.is_

More flake-based magic

Alright, so you've migrated to flakes (right?), you're running home-manager and you've gotten the hang of doing upgrades and adding packages. Maybe you're perfectly happy with things the way they are, or maybe you want more. Fine, here's more flake magic:

Keeping up with fast-moving projects

There are some projects that move very quickly. Take, for example, yt-dlp. This project, written in Python, provides a way to download videos from popular video sites for offline consumption. Some folks use it to backup the YouTube videos they've posted. It works by directly accessing sites through scraping or using exposed APIs. The sites it supports are constantly changing, though, meaning that sometimes only the absolute latest version actually works.

The maintainers of yt-dlp in nixpkgs do an excellent job, but they presumably have to sleep sometime, so more than once I've found yt-dlp broken for what I need to do with it. In most distros, this would be a hurry-up-and-wait situation where you need the package maintainer to pull in upstream changes and then publish updated packages. Some distros move slowly enough that you'd need to add an external package repository just to keep up.

Since NixOS focuses on building from source, it's really easy to build yt-dlp at the absolute latest bleeding edge version rather than the version last referenced in nixpkgs.

(Prerequisite) Add yt-dlp to your environment

Something like this if you want it system-wide:

environment.systemPackages = with pkgs; [
  yt-dlp
];

Add a flake reference

{
  inputs = [
    yt-dlp-github = {
      url = "github:yt-dlp/yt-dlp";
      flake = false; # see note below
    };
  ];
}

The flake = false; bit above tells Nix that it should not expect to find a flake.nix in the referenced repo. Some projects (e.g. hercules-ci/arion) are consumable as flakes, but yt-dlp is not one of them.

Update your flake references

nix flake update

You'll notice in the output that some new references have been added.

Update your nixosConfiguration

Now you'll want to update your nixosConfiguration in order to point yt-dlp to its new source:

nixpkgs.config.packageOverrides = pkgs: rec {
  yt-dlp = pkgs.yt-dlp.overridePythonAttrs(old: {
    src = inputs.yt-dlp-github;
  });
};

Because yt-dlp is a Python project, its overrides are a bit different than you might see for some other types of package. Derivations have a src attribute that contains things like a URL and a source hash. In our case, the flake reference has those same attributes, so we can simply override the src attr with our new flake reference.

Time to actually build the thing

Now that you've got your override in place, you need to build:

nixos-rebuild switch .#your-hostname

This will take a bit more time than usual since the yt-dlp project is built on your machine instead of simply copying a binary version from a build cache.

But it's worth it.

Styling, but make it Nix

I tend to prefer gruvbox-dark as a color scheme for most things, but I only have so much time in the day to dedicate to getting my colors into all of my apps. Thankfully, someone else has already spent a bunch of time getting theming working across a bunch of different environments and apps in Nix… the project is called Stylix and it provides a nice Nixy way to do styling.

I won't dive too deep into what you can do with it, but here's a little walkthough:

Installation

{
  inputs = [
    stylix.url = "github:danth/stylix";
  ];
}

Don't forget to nix flake update.

{
  modules = [
    stylix.nixosModules.stylix
  ];
}

Configuration

First things first, you need to enable stylix in your nixosConfiguration.

{
  stylix.enable = true;
}

Of course, that doesn't really do much right now.

Choosing a scheme

You can choose your color palette in one of a few ways. The first is to just set a wallpaper image you like:

{
  stylix = {
    enable = true;
    image = pkgs.fetchurl {
      # don't blame me, this is from the stylix sample :)
      url = "https://www.pixelstalk.net/wp-content/uploads/2016/05/Epic-Anime-Awesome-Wallpapers.jpg"; 
      sha256 = "enQo3wqhgf0FEPHj2coOCvo7DuZv+x5rL/WIo4qPI50=";
    };
  };
}

When a background is specified, but no theme is chosen, then stylix generates a theme based on colors present in the image. Neat, right?

If you don't want those colors, you can choose a scheme from one of the Tinted Theming set like so:

{
  stylix = {
    enable = true;
    base16Scheme = "${pkgs.base16-schemes}/share/themes/gruvbox-dark-hard.yaml";
  };
}

You can also configure fonts, cursors, and more as well as override individual colors to your preference. More details can be found here.

Try it out!

Now that you've gotten your stylix config in place, so the standard nixos-rebuild dance to try out your new color scheme. You may need to logout/login or reboot to get all of the styling.

Everybody do the wallpaper shuffle

Who doesn't love a good wallpaper? I do, but I don't like to think too hard about it. I found a GitHub repo containing a decent collection of gruvbox-dark wallpapers and wanted to pull them in, but I didn't want to just download a zip, extract, and apply. It's 2024 and I already have flake.nix open, so…

Flake it!

{
  inputs = [
    gruvbox-wallpapers = {
      url = "github:AngelJumbo/gruvbox-wallpapers";
      flake = false;
    };
  ];
};

Do a flake update and all of these wallpapers will become referencable.

But how to set them as backgrounds? I couldn't choose between all of the them, but liked the "minimalistic" wallpapers the most.

Random wallpapers!

home-manager has a random-background service that will rotate through wallpapers for you, so let's set that up!

{
  services.random-background = {
    enable = true;
    interval = "15m"; # how often the background is swapped
    imageDirectory = "${inputs.gruvbox-wallpapers}/wallpapers/minimalistic";
  };
}

Do you rebuild and log out/in. You should now see random wallpapers pulled from GitHub via flake.

The End

There you have it, some little tricks to help expand your mind as you continue your Nix journey.