1
0
Fork 0

Add common/lib.nix

This commit is contained in:
Malte Brandy 2019-07-24 17:24:29 +02:00
parent a49e5ab33a
commit fb4e422936
No known key found for this signature in database
GPG key ID: 226A2D41EF5378C9
5 changed files with 184 additions and 2 deletions

43
common/lib.nix Normal file
View file

@ -0,0 +1,43 @@
let
pkgs = import <nixpkgs> {};
unstable = import <unstable> {};
shh = unstable.haskell.lib.overrideCabal unstable.haskellPackages.shh (drv: {
broken = false;
doCheck = false;
});
writeHaskellScript = { name ? "haskell-script", bins ? [pkgs.coreutils], libraries ? [], imports ? []}: code:
unstable.writers.writeHaskellBin name { libraries = libraries ++ [shh]; } ''
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TemplateHaskell #-}
import Shh
${builtins.concatStringsSep "\n" (map (x: "import ${x}") imports)}
-- Load binaries from Nix packages. The dependencies will be captured
-- in the closure.
loadFromBins ["${builtins.concatStringsSep ''", "'' (builtins.map toString bins)}"]
${code}
'';
in {
inherit writeHaskellScript;
getNivPath = writeHaskellScript {
name = "get-niv-path";
bins = [pkgs.nix pkgs.coreutils];
imports = ["System.Console.CmdArgs.Implicit" "Data.Text.Lazy" "Data.Text.Lazy.Encoding"];
libraries = [ unstable.haskellPackages.cmdargs unstable.haskellPackages.text ];
} ''
data Path = Path {
sources :: String,
channel :: String
} deriving (Show, Data, Typeable)
path = Path{ sources = def &= argPos 0, channel = def &= argPos 1}
main :: IO ()
main = do
path <- cmdArgs path
path <- readTrim $ nix_instantiate "--eval" "-E" ("toString (import " ++ sources path ++ ")." ++ channel path)
echo . unpack . dropAround ('"' ==) $ decodeUtf8 path
'';
}

View file

@ -1,6 +1,6 @@
{ pkgs, config, ... }:
let
inherit (import ../nix/my-lib.nix) writeHaskellScript getNivPath;
inherit (import ../common/my-lib.nix) writeHaskellScript getNivPath;
sources = import ../nix/sources.nix;
inherit (config.m-0.private) me meWork;
configPath = "/home/${config.home.username}/git/nixos/config";

50
nix/sources.json Normal file
View file

@ -0,0 +1,50 @@
{
"home-manager": {
"branch": "release-19.03",
"description": "Manage a user environment using Nix",
"homepage": "",
"owner": "rycee",
"repo": "home-manager",
"rev": "4f13f06b016d59420cafe34915abdd6b795d3416",
"sha256": "1chl04jimba1n1r4cqaq66kry4fy2xgsdgbqcl3rkw4ykd5yfcsc",
"type": "tarball",
"url": "https://github.com/rycee/home-manager/archive/4f13f06b016d59420cafe34915abdd6b795d3416.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"niv": {
"branch": "master",
"description": "Easy dependency management for Nix projects",
"homepage": "https://github.com/nmattia/niv",
"owner": "nmattia",
"repo": "niv",
"rev": "8b7b70465c130d8d7a98fba1396ad1481daee518",
"sha256": "0fgdrxn2vzpnzr6pxaiyn5zzbd812c6f7xjjhfir0kpzamjnxwwl",
"type": "tarball",
"url": "https://github.com/nmattia/niv/archive/8b7b70465c130d8d7a98fba1396ad1481daee518.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "nixos-19.03",
"description": "Nixpkgs/NixOS branches that track the Nixpkgs/NixOS channels",
"homepage": null,
"owner": "NixOS",
"repo": "nixpkgs-channels",
"rev": "55b8860aa209e987f6f15c523811e4861d97d6af",
"sha256": "0ri58704vwv6gnyw33vjirgnvh2f1201vbflk0ydj5ff7vpyy7hf",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs-channels/archive/55b8860aa209e987f6f15c523811e4861d97d6af.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"unstable": {
"branch": "nixos-unstable",
"description": "Nixpkgs/NixOS branches that track the Nixpkgs/NixOS channels",
"homepage": null,
"owner": "NixOS",
"repo": "nixpkgs-channels",
"rev": "362be9608c3e0dc5216e9d1d5f5c1a5643b7f7b1",
"sha256": "0934rhanamsnhawg15gg6cy9ird3c47hsqn5s46lq2n5kzl6v7ly",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs-channels/archive/362be9608c3e0dc5216e9d1d5f5c1a5643b7f7b1.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}

89
nix/sources.nix Normal file
View file

@ -0,0 +1,89 @@
# This file has been generated by Niv.
# A record, from name to path, of the third-party packages
with rec
{
pkgs =
if hasNixpkgsPath
then
if hasThisAsNixpkgsPath
then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}
else import <nixpkgs> {}
else
import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {};
sources_nixpkgs =
if builtins.hasAttr "nixpkgs" sources
then sources.nixpkgs
else abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball =
{ url, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball { inherit url; }
else
fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl =
{ url, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl { inherit url; }
else
fetchurl attrs;
# A wrapper around pkgs.fetchzip that has inspectable arguments,
# annoyingly this means we have to specify them
fetchzip = { url, sha256 }@attrs: pkgs.fetchzip attrs;
hasNixpkgsPath = (builtins.tryEval <nixpkgs>).success;
hasThisAsNixpkgsPath =
(builtins.tryEval <nixpkgs>).success && <nixpkgs> == ./.;
sources = builtins.fromJSON (builtins.readFile ./sources.json);
mapAttrs = builtins.mapAttrs or
(f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)));
# borrowed from nixpkgs
functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
callFunctionWith = autoArgs: f: args:
let auto = builtins.intersectAttrs (functionArgs f) autoArgs;
in f (auto // args);
getFetcher = spec:
let fetcherName =
if builtins.hasAttr "type" spec
then builtins.getAttr "type" spec
else "builtin-tarball";
in builtins.getAttr fetcherName {
"tarball" = fetchzip;
"builtin-tarball" = builtins_fetchTarball;
"file" = pkgs.fetchurl;
"builtin-url" = builtins_fetchurl;
};
};
# NOTE: spec must _not_ have an "outPath" attribute
mapAttrs (_: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec
then
spec //
{ outPath = callFunctionWith spec (getFetcher spec) { }; }
else spec
) sources

View file

@ -1,6 +1,6 @@
{ pkgs, config, lib, ... }:
let
inherit (import ../nix/my-lib.nix) writeHaskellScript getNivPath;
inherit (import ../common/my-lib.nix) writeHaskellScript getNivPath;
me = config.m-0.private.me;
sources = import ../nix/sources.nix;
nixpkgsPath = sources.nixpkgs;