nixでmacにclamavを入れる

概要

darwin向けのclamavがnixpkgsにないのでnixファイルを作りながらやっていきます。

手法

nixpkgsのfileを見ます。

nixpkgs/default.nix at 00b555ef889b900c32c36ef9ef7fa3fb725ae4b5 · NixOS/nixpkgs · GitHub

これですね。

このブログを時間がたってから読んだ人はmasterを見た方がいいかもしれません。

このままでは失敗するので修正していきます。

まず失敗する理由として, derivationのmetaのplatformがlinuxになっているのでdarwinにします。

    platforms = platforms.darwin;

適当に修正しながらやっていくと以下のようなfileになると思います。

{ stdenv, fetchurl, pkgconfig
, gcc
, zlib, bzip2, libiconv, libxml2, openssl, ncurses, curl, pcre2
, libmspack
}:

stdenv.mkDerivation rec {
  name = "clamav-${version}";
  version = "0.100.2";

  src = fetchurl {
    url = "https://www.clamav.net/downloads/production/${name}.tar.gz";
    sha256 = "1mkd41sxbjkfjinpx5b9kb85q529gj2s3d0klysssqhysh64ybja";
  };

  # don't install sample config files into the absolute sysconfdir folder
  postPatch = ''
    substituteInPlace Makefile.in --replace ' etc ' ' '
  '';
  nativeBuildInputs = [ pkgconfig ];
  buildInputs = [
    zlib bzip2 libxml2 openssl ncurses curl libiconv pcre2 libmspack gcc
  ];

  configureFlags = [
    "--libdir=$(out)/lib"
    "--sysconfdir=/etc/clamav"
    "--disable-llvm" # enabling breaks the build at the moment
    "--with-zlib=${zlib.dev}"
    "--with-xml=${libxml2.dev}"
    "--with-openssl=${openssl.dev}"
    "--with-libcurl=${curl.dev}"
    "--with-system-libmspack"
    "--disable-clamav"
    # "--enable-milter"
  ];

  postInstall = ''
    mkdir $out/etc
    cp etc/*.sample $out/etc
  '';

  meta = with stdenv.lib; {
    homepage = https://www.clamav.net;
    description = "Antivirus engine designed for detecting Trojans, viruses, malware and other malicious threats";
    license = licenses.gpl2;
    platforms = platforms.darwin;
  };
}

--enable-milterはちょっとメール関係の機能っぽい?かつビルドが成功しなかったので消しました。

--disable-clamavをconfigure flagに入れておかないと ./configureした時にuserとgroupを作られるのでこのオプションはつけた方がいいと思います。(流石にマジかって気持ちになりました)

その代わり以下の操作をする必要があります。

clamav-faq/Steps-macOS.md at master · Cisco-Talos/clamav-faq · GitHub

ただ nix-darwinを使っている人間は以下の操作をすることで、上記の操作の一定部分を回避できます。

{config, pkgs,}:
  let
  clamav = pkgs.callPackage ./clamav.nix {};
  stateDir = "/var/lib/clamav";
  runDir = "/run/clamav";
  clamavUser = "clamav";
  clamdConfigFile = pkgs.writeText "clamd.conf" ''
    DatabaseDirectory ${stateDir}
    LocalSocket ${runDir}/clamd.ctl
    PidFile ${runDir}/clamd.pid
    TemporaryDirectory /tmp
    User ${clamavUser}
    Foreground yes
    ExcludePath ^/System/
    ExcludePath ^/Volumes/
    ExcludePath ^/Network/
    ExcludePath ^/bin/
    ExcludePath ^/sbin/
    ExcludePath ^/nix/
  '';

  freshclamConfigFile = pkgs.writeText "freshclam.conf" ''
    DatabaseDirectory ${stateDir}
    Foreground yes
    Checks 12
    DatabaseMirror database.clamav.net
  '';
  in
{
  environment.etc."clamav/freshclam.conf".source = freshclamConfigFile;
  environment.etc."clamav/clamd.conf".source = clamdConfigFile;

  users.users.clamav.uid = 599;
  users.users.clamav.gid = config.users.groups.clamav.gid;
  users.users.clamav.home = stateDir;
  users.users.clamav.shell = "/bin/false";
  users.users.clamav.description = "clamav service user";

  users.groups.clamav.gid = 799;
  users.groups.clamav.description = "group for clamav service";
  users.knownGroups = [ clamavUser ];
  users.knownUsers = [ clamavUser ];
}

上記は以下のnixos向けの設定を見ることで作ることができます。

github.com

ちょっとlaunchd周りはいじってる途中なので、今度書くかもしれません。