2

I've just started playing with NixOS, and have so far managed to edit /etc/nixos/configuration.nix in my NixOS 18.09 VM to have PHP-FPM and the Caddy webserver enabled.

{ config, pkgs, ... }:

{
  imports = [ <nixpkgs/nixos/modules/installer/virtualbox-demo.nix> ];

  users = {
    mutableUsers = false;
    groups = {
      caddy = { };
      php-project = { };
    };
    users = {
      hello = {
        group = "php-project";
      };
    };
  };

  environment.systemPackages = [
    pkgs.htop  
    pkgs.httpie
    pkgs.php  # for PHP CLI
  ];

  services.caddy = {
    enable = true;
    email = "david@example.com";
    agree = true;
    config = ''
      (common) {
        gzip
        header / -Server
        header / -X-Powered-By
      }
      :8080 {
        root /var/www/hello
        fastcgi / /run/phpfpm/hello.sock php
        log syslog
        import common
      }
    '';
  };

  services.phpfpm = {
    phpOptions = ''
      date.timezone = "Europe/Berlin"
    '';
    poolConfigs = {
      hello = ''
        user = hello
        listen = /run/phpfpm/hello.sock
        ; ...
        pm.max_requests = 500
      '';
    };
  };
}

A PHP-processed response is available at at localhost:8080. (Yay!)

To enable Caddy plugins when compiling from source, Go imports are added to caddy's run.go, e.g.:

_ "github.com/mholt/caddy/caddyhttp" // plug in the HTTP server type
    // This is where other plugins get plugged in (imported)
_ "github.com/nicolasazrak/caddy-cache" // added to use another plugin
)

How can I set such line insertion to be performed after the source is downloaded and before the build takes place? (If this is a reasonable approach when using Nix?)

The NixOS 18.09 caddy package.

The NixOS 18.09 caddy service.

I believe that when writing a package a builder script (Bash or otherwise) can be assigned, and I'm thinking the line insertion could be done in it. But I'm lost as to how to assign a script to an existing package in this situation (override an attribute/use an overlay?) and where to put the script on the disk.


Status update

I've been doing some reading on customising packages in general and it sounds like overlays might be what I need. However, I don't seem to be able to get my overlay evaluated.

I'm using overriding of the package name as a test as it's simpler than patching code.

Overlay attempt 1

/etc/nixos/configuration.nix:

{ config, pkgs, options, ... }:

{
  imports = [ <nixpkgs/nixos/modules/installer/virtualbox-demo.nix> ];

  nix.nixPath = options.nix.nixPath.default ++ [
    "nixpkgs-overlays=/etc/nixos/overlays-compat/"
  ];

  # ...
}

/etc/nixos/overlays-compat/overlays.nix:

self: super:
with super.lib;
let
  # Using the nixos plumbing that's used to evaluate the config...
  eval = import <nixpkgs/nixos/lib/eval-config.nix>;
  # Evaluate the config,
  paths = (eval {modules = [(import <nixos-config>)];})
    # then get the `nixpkgs.overlays` option.
    .config.nixpkgs.overlays
  ;
in
foldl' (flip extends) (_: super) paths self

/etc/nixos/overlays-compat/caddy.nix:

self: super:
{
  caddy = super.caddy.override {
    name = "caddy-override";
  };
}

Overlay attempt 2

/etc/nixos/configuration.nix:

  nixpkgs.overlays = [ (self: super: {
    caddy = super.caddy.override {
      name = "caddy-override";
    };
  } ) ];

error: anonymous function at /nix/store/mr5sfmz6lm5952ch5q6v49563wzylrkx-nixos-18.09.2327.37694c8cc0e/nixos/pkgs/servers/caddy/default.nix:1:1 called with unexpected argument 'name', at /nix/store/mr5sfmz6lm5952ch5q6v49563wzylrkx-nixos-18.09.2327.37694c8cc0e/nixos/lib/customisation.nix:69:12

overrideAttrs

I previously managed to override the package name with this:

{ config, pkgs, options, ... }:

let
  caddyOverride = pkgs.caddy.overrideAttrs (oldAttrs: rec {
    name = "caddy-override-v${oldAttrs.version}";
  });
in {
{
  # ...

  services.caddy = {
    package = caddyOverride;
    # ...
  }
}

I could see in htop that the caddy binary was in a folder called /nix/store/...-caddy-override-v0.11.0-bin/. But I understand that overriding in this way has been superseded by overlays.

David Oliver
  • 2,209
  • 21
  • 33
  • Overlays are for modifying the `pkgs` variable, but that's not necessary because you can already put an arbitrary derivation in `services.caddy.package`. What I can see is mostly plumbing to get your overlays from the `nixpkgs.overlays` option to other places like the `NIX_PATH` environment variable, but I don't see how you define the overlay in `nixpkgs.overlays`. – Robert Hensing Mar 20 '19 at 16:31
  • I understood from the wiki and other pages that all overlay files in the given directory would be used. My idea was to do something like adding a 'patches' attribute to the package via overrideAttrs or an overlay. – David Oliver Mar 20 '19 at 16:52

2 Answers2

0

In order to add plugins to Caddy, it seems that the method is to modify the source.

You will need to adapt the Nixpkgs expression for Caddy to make that possible. That can be done outside the Nixpkgs tree, using services.caddy.package = callPackage ./my-caddy.nix {} for example, or by forking the Nixpkgs repository and pointing your NIX_PATH to your clone.

Robert Hensing
  • 4,412
  • 13
  • 18
  • Thanks! Re. modify source: indeed. Re. fork and modify expression: ah, so a fork is definitely necessary, and I can't "extend" the expression with an overlay? If so, should this be done with 'patches = [ /path/to/patch ]' in my fork? – David Oliver Mar 20 '19 at 16:49
  • Sorry for the mixed signals. I usually fork so I can upstream the code easily, but you can use overlays or `services.caddy.package` instead. – Robert Hensing Mar 21 '19 at 10:45
  • Patches are useful for this. Alternatively you can use the `postPatch` attribute to modify the sources with a script. – Robert Hensing Mar 21 '19 at 10:47
  • Simply modifying the `preBuild` function is not enough to bring in the dependencies into Caddy. You have to also update the `go-modules` derivation to add in the extra plugin as a dependency. However this is documented anywhere and I've not been successful in https://github.com/NixOS/nixpkgs/issues/14671 – CMCDragonkai Dec 19 '19 at 05:48
0

There is an issue for Caddy plugins: https://github.com/NixOS/nixpkgs/issues/14671

PR welcome!

davidak
  • 129
  • 6