Unverified Commit 31687173 authored by lassulus's avatar lassulus Committed by GitHub
Browse files

lib.licenses: add compound licenses (#468378)

parents 5a4b444f baf97485
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -268,7 +268,7 @@ pkgs/development/python-modules/buildcatrust/ @ajs124 @lukegb @mweinelt
/pkgs/applications/editors/jetbrains @leona-ya @theCapypara

# Licenses
/lib/licenses.nix @alyssais @emilazy @jopejoe1
/lib/licenses @alyssais @emilazy @jopejoe1

# Qt
/pkgs/development/libraries/qt-5 @K900 @NickCao @SuperSandro2000 @ttuegel
+1 −1
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ let
      types = callLibs ./types.nix;

      # constants
      licenses = callLibs ./licenses.nix;
      licenses = callLibs ./licenses;
      sourceTypes = callLibs ./source-types.nix;
      systems = callLibs ./systems;

+7 −0
Original line number Diff line number Diff line
{ lib }:
let
  licenses = import ./licenses.nix { inherit lib; };
  operators = import ./operators.nix;
  helpers = import ./helpers.nix { inherit lib; };
in
licenses // operators // helpers
+152 −0
Original line number Diff line number Diff line
{ lib }:
rec {
  /**
    Evaluate a license expression for a given predicate.

    # Example

    ```nix
    evaluateProperty (x: x.free) true (with lib.licenses; AND [ ncsa (WITH asl20 llvm-exception) ])
    ```
    # Type

    ```
    evaluateProperty :: Function -> Bool -> AttrSet -> Bool
    ```

    # Arguments

    - [predicate] checks for each license included in the license expression
    - [permissive] whether to apply checks permissive or reciprocal
    - [license] license expression to check
  */
  evaluateProperty =
    predicate: permissive: license:
    let
      OR = if permissive then lib.any else lib.all;
      AND = if permissive then lib.all else lib.any;
    in
    if license.licenseType == "simple" then
      predicate license
    else if license.licenseType == "compound" then
      if license.operator == "OR" then
        OR (x: evaluateProperty predicate permissive x) license.licenses
      else if license.operator == "AND" then
        AND (x: evaluateProperty predicate permissive x) license.licenses
      else
        throw "Unknown license operator"
    else if license.licenseType == "exception" then
      AND (x: evaluateProperty predicate permissive x) [
        license.license
        license.exception
      ]
    else if license.licenseType == "plus" then
      evaluateProperty predicate permissive license.license
    else
      throw "Unknown license type or legacy license";

  /**
    Check whether a license expression is free.

    # Example

    ```nix
    isFree (with lib.licenses; (AND [ ncsa (WITH asl20 llvm-exception) ]))
    => true
    ```

    # Type

    ```
    isFree :: AttrSet -> Bool
    ```

    # Arguments

    - [license] License expression to check if free
  */
  isFree = evaluateProperty (x: x.free) true;

  /**
    Check whether a license expression is redistributable.

    # Example

    ```nix
    isRedistributable (with lib.licenses; (AND [ ncsa (WITH asl20 llvm-exception) ]))
    => true
    ```

    # Type

    ```
    isRedistributable :: AttrSet -> Bool
    ```

    # Arguments

    - [license] License expression to check if redistributable
  */
  isRedistributable = evaluateProperty (x: x.redistributable) true;

  /**
    Check whether any of the given licenses is required in the license expression.

    # Example

    ```nix
    containsLicenses [ lib.licenses.asl20 ] (with lib.licenses; (AND [ ncsa (WITH asl20 llvm-exception) ]))
    => true
    ```

    # Type

    ```
    containsLicenses :: List -> AttrSet -> Bool
    ```

    # Arguments

    - [licenses] List of licenses to look
    - [license] License expression to check
  */
  containsLicenses = licenses: evaluateProperty (x: lib.lists.elem x licenses) false;

  /**
    Convert a license expression to an SPDX license expression string.

    # Example

    ```nix
    toSPDX (with lib.licenses; AND [ ncsa (WITH asl20 llvm-exception) ])
    => "NCSA AND (Apache-2.0 WITH LLVM-exception)"
    ```

    # Type

    ```
    toSPDX :: AttrSet -> String
    ```

    # Arguments

    - [license] License expression which to convert to spdx expression
  */
  toSPDX =
    license:
    let
      mkBracket =
        x:
        if x.licenseType == "compound" || x.licenseType == "exception" then "(${toSPDX x})" else toSPDX x;
    in
    if license.licenseType == "simple" then
      license.spdxId or "LicenseRef-nixos-${license.shortName}"
    else if license.licenseType == "compound" then
      lib.concatMapStringsSep " ${license.operator} " (x: mkBracket x) license.licenses
    else if license.licenseType == "exception" then
      "${mkBracket license.license} ${license.operator} ${mkBracket license.exception}"
    else if license.licenseType == "plus" then
      "${mkBracket license.license}${license.operator}"
    else
      throw "Unknown license type";
}
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ let
        deprecated
        redistributable
        ;
      licenseType = "simple";
    }
    // optionalAttrs (attrs ? spdxId) {
      inherit spdxId;
Loading