From fb4e4229361f1d5de9acf7738b288dbc05eb44a0 Mon Sep 17 00:00:00 2001 From: Malte Brandy Date: Wed, 24 Jul 2019 17:24:29 +0200 Subject: [PATCH] Add common/lib.nix --- common/lib.nix | 43 +++++++++++++++++++ home-manager/default.nix | 2 +- nix/sources.json | 50 ++++++++++++++++++++++ nix/sources.nix | 89 ++++++++++++++++++++++++++++++++++++++++ system/default.nix | 2 +- 5 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 common/lib.nix create mode 100644 nix/sources.json create mode 100644 nix/sources.nix diff --git a/common/lib.nix b/common/lib.nix new file mode 100644 index 00000000..ec10e334 --- /dev/null +++ b/common/lib.nix @@ -0,0 +1,43 @@ +let + pkgs = import {}; + unstable = import {}; + 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 + ''; +} diff --git a/home-manager/default.nix b/home-manager/default.nix index 37e5f5df..fea97c66 100644 --- a/home-manager/default.nix +++ b/home-manager/default.nix @@ -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"; diff --git a/nix/sources.json b/nix/sources.json new file mode 100644 index 00000000..09f804ac --- /dev/null +++ b/nix/sources.json @@ -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///archive/.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///archive/.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///archive/.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///archive/.tar.gz" + } +} diff --git a/nix/sources.nix b/nix/sources.nix new file mode 100644 index 00000000..7a9a2ae3 --- /dev/null +++ b/nix/sources.nix @@ -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 {} + else + import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}; + + sources_nixpkgs = + if builtins.hasAttr "nixpkgs" sources + then sources.nixpkgs + else abort + '' + Please specify either (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 ).success; + hasThisAsNixpkgsPath = + (builtins.tryEval ).success && == ./.; + + 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 diff --git a/system/default.nix b/system/default.nix index 393827af..834427a4 100644 --- a/system/default.nix +++ b/system/default.nix @@ -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;