summaryrefslogtreecommitdiffstats
path: root/makefu/3modules/sane-extra.nix
blob: 2e0ce8f2fc8104e777d55166da44bdd01c658853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{ config, lib, pkgs, ... }:
# https://github.com/michalrus/dotfiles/blob/d943be3089aa436e07cea5f22d829402936a9229/.nixos-config.symlink/modules/sane-extra-config.nix
# via https://github.com/NixOS/nixpkgs/issues/17411
# via  https://unix.stackexchange.com/questions/321954/install-epson-v39-on-nixos
with lib;

let

  cfg = config.hardware.sane;

  pkg = if cfg.snapshot
    then pkgs.sane-backends-git
    else pkgs.sane-backends;

  backends = [ pkg ] ++ cfg.extraBackends;

  saneConfig = pkgs.mkSaneConfig { paths = backends; };

  saneExtraConfig = pkgs.runCommand "sane-extra-config" {} ''
    cp -Lr '${pkgs.mkSaneConfig { paths = [ pkgs.sane-backends ]; }}'/etc/sane.d $out
    chmod +w $out
    ${concatMapStrings (c: ''
      f="$out/${c.name}.conf"
      [ ! -e "$f" ] || chmod +w "$f"
      cat ${builtins.toFile "" (c.value + "\n")} >>"$f"
      chmod -w "$f"
    '') (mapAttrsToList nameValuePair cfg.extraConfig)}
    chmod -w $out
  '';

in

{
  options = {
    hardware.sane.extraConfig = mkOption {
      type = types.attrsOf types.lines;
      default = {};
      example = { "some-backend" = "# some lines to add to its .conf"; };
    };
  };

  config = mkIf (cfg.enable && cfg.extraConfig != {}) {
    hardware.sane.configDir = saneExtraConfig.outPath;
  };
}