Use with nix flakes

13 Sep 2021 - Gabriel Fontes

I’ve recently started migrating from github to sourcehut, and i’ve been having a blast.

Here’s a quick write up on how to use the awesome CI with your shiny nix flake-based project.


First of all, of course, your project needs a flake.nix. More specifically, your flake needs a to be built with nix build xxx (as a plus, set your preferred packaged to outputs.defaultPackage, so you can build with nix build). Here’s how this website’s looks like:

  description = "My personal website, blog, and digital garden";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
        pkgs = import nixpkgs { inherit system; };
        package = "misterio-me";
      in {
        # Package, specifies what nix builds with 'nix build', in this case builds the website with jekyll
        packages.${package} = pkgs.stdenv.mkDerivation {
          pname = package;
          version = "1.0";
          src = ./.;
          buildPhase = ''
            JEKYLL_ENV=production ${pkgs.jekyll}/bin/jekyll build --destination $out
          installPhase = "true";
        defaultPackage = self.packages.${system}."${package}";

        # App, specifies what nix does with 'nix run', in this case serves up the website
        apps.${package} = let
          serve = pkgs.writeShellScriptBin "serve" ''
            echo "Serving on:"
            ${pkgs.webfs}/bin/webfsd -f index.html -F -p 4000 -r ${self.packages.${system}.${package}}
        in {
          type = "app";
          program = "${serve}/bin/serve";
        defaultApp = self.apps.${system}.${package};

        # Development shell, specifies what nix provides with 'nix develop'
        devShell =
          pkgs.mkShell { buildInputs = with pkgs; [ jekyll nodePackages.prettier sass scss-lint ]; };

I’ve included it all as a reference, but you can safely ignore apps and devShell, if you don’t need them.

Build manifest

Minimal example

Here’s what you’re probably looking for. This is a minimal .build.yml manifest is how you can easily get some sweet nix flakes support on your runner:

image: nixos/unstable
- nixos.nixUnstable
  NIX_CONFIG: "experimental-features = nix-command flakes"

This baby will install nixUnstable using nix-env, and add the required experimental features to your environment (so you don’t have to edit a file or use cli arguments for that).

How i use it

Just add your tasks and environment entries as needed. I build and deploy this website to SourceHut pages with this manifest:

image: nixos/unstable
- nixos.nixUnstable
  NIX_CONFIG: "experimental-features = nix-command flakes"


- build: |
    nix --quiet build
- package: |
    tar -C -cvz . > site.tar.gz
- upload: |
    acurl -f -Fcontent=@site.tar.gz

Pretty sweet!

Articles from blogs I like :)

Generated by openring