From 3cd02da1ce9a9d6ac851f01e3628d1ee15c5a0e4 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Sun, 7 Apr 2019 19:56:40 +0200 Subject: [PATCH 1/3] Fix sources.nix for empty and ./. --- nix/sources.nix | 58 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/nix/sources.nix b/nix/sources.nix index c0941e9..051faaf 100644 --- a/nix/sources.nix +++ b/nix/sources.nix @@ -1,20 +1,58 @@ +# This file has been generated by Niv. + # A record, from name to path, of the third-party packages -with +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. + ''; + + builtins_fetchTarball = + # fetchTarball version that is compatible between all the versions of + # Nix + { url, sha256 }@attrs: + let + inherit (builtins) lessThan nixVersion fetchTarball; + in + if lessThan nixVersion "1.12" then + fetchTarball { inherit url; } + else + fetchTarball attrs; + + hasNixpkgsPath = (builtins.tryEval ).success; + hasThisAsNixpkgsPath = + (builtins.tryEval ).success && == ./.; + sources = builtins.fromJSON (builtins.readFile ./sources.json); - # fetchTarball version that is compatible between all the sources of Nix - fetchTarball = - { url, sha256 }: - if builtins.lessThan builtins.nixVersion "1.12" then - builtins.fetchTarball { inherit url; } - else - builtins.fetchTarball { inherit url sha256; }; mapAttrs = builtins.mapAttrs or (f: set: with builtins; listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))); -}; + getFetcher = spec: + let fetcherName = + if builtins.hasAttr "type" spec + then builtins.getAttr "type" spec + else "tarball"; + in builtins.getAttr fetcherName { + "tarball" = pkgs.fetchzip; + "file" = pkgs.fetchurl; + }; +}; # NOTE: spec must _not_ have an "outPath" attribute mapAttrs (_: spec: if builtins.hasAttr "outPath" spec @@ -24,6 +62,6 @@ mapAttrs (_: spec: if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec then spec // - { outPath = fetchTarball { inherit (spec) url sha256; } ; } + { outPath = getFetcher spec { inherit (spec) url sha256; } ; } else spec ) sources From e45c61ecdc4b927d713b7b4a3df3383d0c17f31a Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Sun, 7 Apr 2019 19:56:58 +0200 Subject: [PATCH 2/3] Use niv's nix/sources.nix in niv --- app/Niv.hs | 69 ++++++++++++++-------------------------------------- default.nix | 2 ++ package.yaml | 3 +++ 3 files changed, 23 insertions(+), 51 deletions(-) diff --git a/app/Niv.hs b/app/Niv.hs index 2603ab0..22a7b6d 100644 --- a/app/Niv.hs +++ b/app/Niv.hs @@ -1,4 +1,5 @@ {-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} @@ -11,6 +12,7 @@ import Control.Monad import Control.Monad.State import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey) import Data.Char (isSpace, toUpper) +import Data.FileEmbed (embedFile) import Data.Functor ((<&>)) import Data.Hashable (Hashable) import Data.Maybe (mapMaybe, fromMaybe) @@ -23,6 +25,7 @@ import UnliftIO import qualified Data.Aeson as Aeson import qualified Data.Aeson.Encode.Pretty as AesonPretty import qualified Data.ByteString as B +import qualified Data.ByteString.Char8 as B8 import qualified Data.ByteString.Lazy as L import qualified Data.HashMap.Strict as HMap import qualified Data.Text as T @@ -319,7 +322,7 @@ cmdInit = do if shouldUpdateNixSourcesNix content then do putStrLn "Updating sources.nix" - writeFile path initNixSourcesNixContent + B.writeFile path initNixSourcesNixContent else putStrLn "Not updating sources.nix" ) , ( pathNixSourcesJson @@ -336,14 +339,14 @@ cmdInit = do , \path _content -> dontCreateFile path) ] $ \(path, onCreate, onUpdate) -> do exists <- Dir.doesFileExist path - if exists then readFile path >>= onUpdate path else onCreate path + if exists then B.readFile path >>= onUpdate path else onCreate path where - createFile :: FilePath -> String -> IO () + createFile :: FilePath -> B.ByteString -> IO () createFile path content = do let dir = takeDirectory path Dir.createDirectoryIfMissing True dir putStrLn $ "Creating " <> path - writeFile path content + B.writeFile path content dontCreateFile :: FilePath -> IO () dontCreateFile path = putStrLn $ "Not creating " <> path @@ -611,24 +614,24 @@ nixPrefetchURL unpack url = -- | Checks if content is different than default and if it does /not/ contain -- a comment line with @niv: no_update@ -shouldUpdateNixSourcesNix :: String -> Bool +shouldUpdateNixSourcesNix :: B.ByteString -> Bool shouldUpdateNixSourcesNix content = content /= initNixSourcesNixContent && - not (any lineForbids (lines content)) + not (any lineForbids (B8.lines content)) where - lineForbids :: String -> Bool + lineForbids :: B8.ByteString -> Bool lineForbids str = - case dropWhile isSpace str of - '#':rest -> case dropWhile isSpace rest of - 'n':'i':'v':':':rest' -> case dropWhile isSpace rest' of - 'n':'o':'_':'u':'p':'d':'a':'t':'e':_ -> True + case B8.uncons (B8.dropWhile isSpace str) of + Just ('#',rest) -> case B8.stripPrefix "niv:" (B8.dropWhile isSpace rest) of + Just rest' -> case B8.stripPrefix "no_update" (B8.dropWhile isSpace rest') of + Just{} -> True _ -> False _ -> False _ -> False warnIfOutdated :: IO () warnIfOutdated = do - tryAny (readFile pathNixSourcesNix) >>= \case + tryAny (B.readFile pathNixSourcesNix) >>= \case Left e -> putStrLn $ unlines [ "Could not read " <> pathNixSourcesNix , "Error: " <> show e @@ -650,51 +653,15 @@ pathNixSourcesNix :: FilePath pathNixSourcesNix = "nix" "sources.nix" -- | Glue code between nix and sources.json -initNixSourcesNixContent :: String -initNixSourcesNixContent = [s| -# This file has been generated by Niv. - -# A record, from name to path, of the third-party packages -with rec -{ - pkgs = import {}; - - 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))); - - getFetcher = spec: - let fetcherName = - if builtins.hasAttr "type" spec - then builtins.getAttr "type" spec - else "tarball"; - in builtins.getAttr fetcherName { - "tarball" = pkgs.fetchzip; - "file" = pkgs.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 = getFetcher spec { inherit (spec) url sha256; } ; } - else spec - ) sources -|] +initNixSourcesNixContent :: B.ByteString +initNixSourcesNixContent = $(embedFile "nix/sources.nix") -- | @nix/sources.json" pathNixSourcesJson :: FilePath pathNixSourcesJson = "nix" "sources.json" -- | Empty JSON map -initNixSourcesJsonContent :: String +initNixSourcesJsonContent :: B.ByteString initNixSourcesJsonContent = "{}" ------------------------------------------------------------------------------- diff --git a/default.nix b/default.nix index 0b22727..2c46a64 100644 --- a/default.nix +++ b/default.nix @@ -4,6 +4,8 @@ with rec [ "^package.yaml$" "^app.*$" "^README.md$" + "^nix$" + "^nix.sources.nix$" ]; haskellPackages = pkgs.haskellPackages.override { overrides = _: haskellPackages: diff --git a/package.yaml b/package.yaml index 9ec6318..3a7c28b 100644 --- a/package.yaml +++ b/package.yaml @@ -11,6 +11,7 @@ executable: dependencies: - base - hashable + - file-embed - process - text - bytestring @@ -24,3 +25,5 @@ executable: - optparse-applicative - unliftio - unordered-containers + data-files: + - nix/sources.nix From b63863299396d67fa49d10f1d3da9cf193585307 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Wed, 10 Apr 2019 23:10:46 +0200 Subject: [PATCH 3/3] Improve error message when no nixpkgs --- nix/sources.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nix/sources.nix b/nix/sources.nix index 051faaf..dc040f8 100644 --- a/nix/sources.nix +++ b/nix/sources.nix @@ -7,10 +7,10 @@ with rec if hasNixpkgsPath then if hasThisAsNixpkgsPath - then import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {} + then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {} else import {} else - import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {}; + import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}; sources_nixpkgs = if builtins.hasAttr "nixpkgs" sources