From b29073af25cb0ff2d8d9a0aa1f958316904d3dc9 Mon Sep 17 00:00:00 2001
From: Nicolas Pierron <nicolas.b.pierron@gmail.com>
Date: Sun, 22 Nov 2009 17:04:33 +0000
Subject: [PATCH] * Add an adapter which abort an install if the installed
 package depends   on a derivation with a meta.license attribute which does
 not satisfy the   license predicate.

With this adapter you can abort any install which depends on software
which are not free by default.  You can try it with MPlayer, because
MPlayer depends of win32codecs flagged as "unfree".

svn path=/nixpkgs/trunk/; revision=18530
---
 pkgs/stdenv/adapters.nix | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/pkgs/stdenv/adapters.nix b/pkgs/stdenv/adapters.nix
index 410cc16838d1..88193714f61b 100644
--- a/pkgs/stdenv/adapters.nix
+++ b/pkgs/stdenv/adapters.nix
@@ -227,4 +227,44 @@ rec {
           drvPath = printDrvPath pkg.drvPath;
         };
     };
+
+  /* Abort if the license predicate is not verified for a derivation
+     declared with mkDerivation.
+
+     One possible predicate to avoid all non-free packages can be achieved
+     with the following function:
+
+     isFree = license: with builtins;
+       if isNull license then true
+       else if isList license then lib.all isFree license
+       else license != "non-free" && license != "unfree";
+
+     This adapter can be defined on the defaultStdenv definition.  You can
+     use it by patching the all-packages.nix file or by using the override
+     feature of ~/.nixpkgs/config.nix .
+  */
+  validateLicenses = licensePred: stdenv: stdenv //
+    { mkDerivation = args:
+        let
+          pkg = stdenv.mkDerivation args;
+          license =
+            if pkg ? meta && pkg.meta ? license then
+              pkg.meta.license
+            else
+              null;
+
+          validate = arg:
+            if licensePred license then arg
+            else abort "
+              Error while building ${builtins.unsafeDiscardStringContext pkg.drvPath}:
+              The license predicate is not verified.
+
+              bad license: ${builtins.exprToString license}
+            ";
+
+        in pkg // {
+          outPath = validate pkg.outPath;
+          drvPath = validate pkg.drvPath;
+        };
+    };
 }