summary refs log tree commit diff
path: root/pkgs/tools/nix/nix-init/license.nix
blob: 0a1ad4fce122dc750fb5c0ff31c20bdb56a661fc (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# vendored from src/licenses.nix

{ lib, writeText }:

let
  inherit (lib)
    concatMapAttrs
    filterAttrs
    flip
    id
    intersectLists
    licenses
    mapAttrsToList
    optionalAttrs
    pipe
    warn
    attrNames
    concatStringsSep
    length
    ;

  licenseMap = flip concatMapAttrs licenses
    (k: v: optionalAttrs (v ? spdxId && !v.deprecated) { ${v.spdxId} = k; });

  deprecatedAliases = {
    "AGPL-3.0" = "agpl3Only";
    "BSD-2-Clause-FreeBSD" = "bsd2WithViews";
    "BSD-2-Clause-NetBSD" = "bsd2";
    "GFDL-1.1" = "fdl11Only";
    "GFDL-1.2" = "fdl12Only";
    "GFDL-1.3" = "fdl13Only";
    "GPL-1.0" = "gpl1Only";
    "GPL-1.0+" = "gpl1Plus";
    "GPL-2.0" = "gpl2Only";
    "GPL-2.0+" = "gpl2Plus";
    "GPL-3.0" = "gpl3Only";
    "GPL-3.0+" = "gpl3Plus";
    "LGPL-2.0" = "lgpl2Only";
    "LGPL-2.0+" = "lgpl2Plus";
    "LGPL-2.1" = "lgpl21Only";
    "LGPL-2.1+" = "lgpl21Plus";
    "LGPL-3.0" = "lgpl3Only";
    "LGPL-3.0+" = "lgpl3Plus";
  };

  lints = {
    "deprecated licenses" = intersectLists
      (attrNames licenseMap)
      (attrNames deprecatedAliases);

    "invalid aliases" = attrNames (filterAttrs
      (k: v: licenses.${v}.deprecated or true)
      deprecatedAliases);
  };

  lint = flip pipe
    (flip mapAttrsToList lints (k: v:
      if v == [ ] then
        id
      else
        warn "${k}: ${concatStringsSep ", " v}"));

  inserts = lint (mapAttrsToList
    (k: v: ''  xs.insert("${k}", "${v}");'')
    (deprecatedAliases // licenseMap));
in

writeText "license.rs" ''
  fn get_nix_licenses() -> rustc_hash::FxHashMap<&'static str, &'static str> {
      let mut xs = std::collections::HashMap::with_capacity_and_hasher(
          ${toString (length inserts)},
          Default::default(),
      );
      ${concatStringsSep "\n    " inserts}
      xs
  }
''