Running Isso on NixOS in a Docker container

Vincent Bernat

This short ar­ti­cle doc­u­ments how I run Isso, the com­ment­ing sys­tem used by this blog, in­side a Docker con­tainer on NixOS, a Linux dis­tri­bu­tion built on top of Nix. Nix is a declar­a­tive pack­age man­ager for Linux and other Unix systems.


While NixOS 20.09 in­cludes a deriva­tion for Isso, it is un­for­tu­nately bro­ken and re­lies on Python 2. As I am also us­ing a fork of Isso, I have built my own deriva­tion, heav­ily in­spired by the one in master:1

issoPackage = with pkgs.python3Packages; buildPythonPackage rec {
  pname = "isso";
  version = "custom";

  src = pkgs.fetchFromGitHub {
    # Use my fork
    owner = "vincentbernat";
    repo = pname;
    rev = "vbe/master";
    sha256 = "0vkkvjcvcjcdzdj73qig32hqgjly8n3ln2djzmhshc04i6g9z07j";
  };

  propagatedBuildInputs = [
    itsdangerous
    jinja2
    misaka
    html5lib
    werkzeug
    bleach
    flask-caching
  ];

  buildInputs = [
    cffi
  ];

  checkInputs = [ nose ];

  checkPhase = ''
    ${python.interpreter} setup.py nosetests
  '';
};

I want to run Isso through Gu­ni­corn. To this ef­fect, I build a Python en­vi­ron­ment com­bin­ing Isso and Gu­ni­corn. Then, I can in­voke the lat­ter with "${issoEnv}/​bin/​gunicorn", like with a vir­tual en­vi­ronment.

issoEnv = pkgs.python3.buildEnv.override {
    extraLibs = [
      issoPackage
      pkgs.python3Packages.gunicorn
      pkgs.python3Packages.gevent
    ];
};

Be­fore build­ing a Docker im­age, I also need to spec­ify the con­fig­u­ra­tion file for Isso:

issoConfig = pkgs.writeText "isso.conf" ''
  [general]
  dbpath = /db/comments.db
  host =
    https://vincent.bernat.ch
    http://localhost:8080
  notify = smtp
  […]
'';

NixOS comes with a con­ve­nient tool to build a Docker im­age with­out a Dockerfile:

issoDockerImage = pkgs.dockerTools.buildImage {
  name = "isso";
  tag = "latest";
  extraCommands = ''
    mkdir -p db
  '';
  config = {
    Cmd = [ "${issoEnv}/bin/gunicorn"
            "--name" "isso"
            "--bind" "0.0.0.0:${port}"
            "--worker-class" "gevent"
            "--workers" "2"
            "--worker-tmp-dir" "/dev/shm"
            "--preload"
            "isso.run"
          ];
    Env = [
      "ISSO_SETTINGS=${issoConfig}"
      "SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
    ];
  };
};

Be­cause we re­fer to the issoEnv deriva­tion in config.​Cmd, the whole deriva­tion, in­clud­ing Isso and Gu­ni­corn, is copied in­side the Docker im­age. The same ap­plies to issoConfig, the con­fig­u­ra­tion file we cre­ated ear­lier, and pkgs.​cacert, the deriva­tion con­tain­ing trusted root cer­tifi­cates. The re­sult­ing im­age is 171 MB once in­stalled, which is com­pa­ra­ble to the De­bian Buster im­age gen­er­ated by the of­fi­cial Dockerfile.

NixOS fea­tures an ab­strac­tion to run Docker con­tain­ers. It is not cur­rently doc­u­mented in the NixOS man­ual, but you can look at the source code of the mod­ule for the avail­able op­tions. I chose to use Pod­man in­stead of Docker as the back­end be­cause it does not re­quire run­ning an ad­di­tional daemon.

virtualisation.oci-containers = {
  backend = "podman";
  containers = {
    isso = {
      image = "isso";
      imageFile = issoDockerImage;
      ports = ["127.0.0.1:${port}:${port}"];
      volumes = [
        "/var/db/isso:/db"
      ];
    };
  };
};

A sys­temd unit file is au­to­mat­i­cally cre­ated to run and su­per­vise the container:

$ systemctl status podman-isso.service
● podman-isso.service
     Loaded: loaded (/nix/store/a66gzqqwcdzbh99sz8zz5l5xl8r8ag7w-unit->
     Active: active (running) since Sun 2020-11-01 16:04:16 UTC; 4min 44s ago
    Process: 14564 ExecStartPre=/nix/store/95zfn4vg4867gzxz1gw7nxayqcl>
   Main PID: 14697 (podman)
         IP: 0B in, 0B out
      Tasks: 10 (limit: 2313)
     Memory: 221.3M
        CPU: 10.058s
     CGroup: /system.slice/podman-isso.service
             ├─14697 /nix/store/pn52xgn1wb2vr2kirq3xj8ij0rys35mf-podma>
             └─14802 /nix/store/7vsba54k6ag4cfsfp95rvjzqf6rab865-conmo>

nov. 01 16:04:17 web03 podman[14697]: container init (image=localhost/isso:latest)
nov. 01 16:04:17 web03 podman[14697]: container start (image=localhost/isso:latest)
nov. 01 16:04:17 web03 podman[14697]: container attach (image=localhost/isso:latest)
nov. 01 16:04:19 web03 conmon[14802]: INFO: connected to SMTP server
nov. 01 16:04:19 web03 conmon[14802]: INFO: connected to https://vincent.bernat.ch
nov. 01 16:04:19 web03 conmon[14802]: [INFO] Starting gunicorn 20.0.4
nov. 01 16:04:19 web03 conmon[14802]: [INFO] Listening at: http://0.0.0.0:8080 (1)
nov. 01 16:04:19 web03 conmon[14802]: [INFO] Using worker: gevent
nov. 01 16:04:19 web03 conmon[14802]: [INFO] Booting worker with pid: 8
nov. 01 16:04:19 web03 conmon[14802]: [INFO] Booting worker with pid: 9

As the last step, we con­fig­ure Ng­inx to for­ward re­quests for comments.​luffy.cx to the con­tainer. NixOS pro­vides a sim­ple in­te­gra­tion to grab a Let’s En­crypt cer­tificate.

services.nginx.virtualHosts."comments.luffy.cx" = {
  root = "/data/webserver/comments.luffy.cx";
  enableACME = true;
  forceSSL = true;
  extraConfig = ''
    access_log /var/log/nginx/comments.luffy.cx.log anonymous;
  '';
  locations."/" = {
    proxyPass = "http://127.0.0.1:${port}";
    extraConfig = ''
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_hide_header Set-Cookie;
      proxy_hide_header X-Set-Cookie;
      proxy_ignore_headers Set-Cookie;
    '';
  };
};
security.acme.certs."comments.luffy.cx" = {
  email = lib.concatStringsSep "@" [ "letsencrypt" "vincent.bernat.ch" ];
};

While I still strug­gle with Nix and NixOS, I am con­vinced this is how declar­a­tive in­fra­struc­ture should be done. I like how in one sin­gle file, I can de­fine the deriva­tion to build Isso, the con­fig­u­ra­tion, the Docker im­age, the con­tainer def­i­ni­tion, and the Ng­inx con­fig­u­ra­tion. The Nix lan­guage is used both for build­ing pack­ages and for man­ag­ing con­fig­u­rations.

More­over, the Docker im­age is up­dated au­to­mat­i­cally like a reg­u­lar NixOS host. This solves an is­sue plagu­ing the Docker ecosys­tem: no more stale im­ages! My next step would be to com­bine this ap­proach with No­mad, a sim­ple or­ches­tra­tor to de­ploy and man­age con­tainers.

Up­date (2023-01)

I am cur­rently not us­ing a Docker im­age any­more, but a sys­temd con­tainer. The Nix store is shared with the host and Isso runs as a dy­namic user.