diff --git a/Main.hs b/Main.hs new file mode 100644 index 0000000..451e2d1 --- /dev/null +++ b/Main.hs @@ -0,0 +1,73 @@ +-- TODO: qualified imports +-- TODO: format code + +import Options.Applicative +import Control.Monad +import Data.Semigroup ((<>)) +import System.Directory +import System.FilePath + +fileFetchNix :: FilePath +fileFetchNix = "nix" "fetch.nix" + +fileFetchNixContent :: String +fileFetchNixContent = unlines + [ + + + ] + +fileVersionsJson :: FilePath +fileVersionsJson = "nix" "versions.json" + +fileVersionsJsonContent :: String +fileVersionsJsonContent = unlines + [ + + ] + +cmdInit :: IO () +cmdInit = do + putStrLn "Creating directory nix (if it doesn't exist)" + createDirectoryIfMissing True "nix" + + putStrLn $ "Creating file " <> fileFetchNix <> " (if it doesn't exist)" + fileFetchNixExists <- doesFileExist fileFetchNix + + if fileFetchNixExists + then do + putStrLn $ "Not writing " <> fileFetchNix + putStrLn "(file exists)" + else do + putStrLn $ "Writing " <> fileFetchNix + writeFile fileFetchNix fileFetchNixContent + + putStrLn $ "Creating file " <> fileVersionsJson <> " (if it doesn't exist)" + fileVersionsJsonExists <- doesFileExist fileVersionsJson + + if fileVersionsJsonExists + then do + putStrLn $ "Not writing " <> fileVersionsJson + putStrLn "(file exists)" + else do + putStrLn $ "Writing " <> fileVersionsJson + writeFile fileVersionsJson fileVersionsJsonContent + +cmdAdd :: IO () +cmdAdd = putStrLn "add" + +cmdShow :: IO () +cmdShow = putStrLn "show" + +cmdUpdate :: IO () +cmdUpdate = putStrLn "update" + +opts :: Parser (IO ()) +opts = subparser ( + command "init" (info (pure cmdInit) idm) <> + command "add" (info (pure cmdAdd) idm) <> + command "show" (info (pure cmdAdd) idm) <> + command "update" (info (pure cmdAdd) idm) ) + +main :: IO () +main = join $ execParser (info opts idm) diff --git a/README.md b/README.md index a502877..09fab14 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,17 @@ A tool for dealing with third-party packages in [Nix]. +## Building + +Inside the provided nix shell: + +``` bash +$ # GHCi: +$ snack ghci -p package.yaml +$ # actual build: +$ snack build -p package.yaml +``` + ## Usage ### Global options @@ -39,11 +50,11 @@ in pkgs.hello #### show -`[--branch] [--rev] [--owner] [--repo] `... if no attribute - (br, rev, own, repo) is given, all attributes are shown for ``. - Otherwise the specified attributes are shown. If no package is specified: - ` = `, otherwise `` is set to the specified - packages. +`[--branch] [--rev] [--owner] [--repo] [--attribute ] `... + if no attribute (br, rev, ...) is given, all attributes are shown for + ``. Otherwise the specified attributes are shown. If no package is + specified: ` = `, otherwise `` is set to + the specified packages. #### update @@ -69,9 +80,10 @@ in pkgs.hello } ``` -* `--branch`: specifies `` +* `--branch`: specifies `` (default: master) * `--username `: then `let = ` * `--gitlab`: use gitlab instead of GitHub +* `--attribute `: sets `` to `` **NOTE**: should the URLs be used instead? or more simply, how do we differentiate between Gitlab/GitHub? diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..e61aa12 --- /dev/null +++ b/default.nix @@ -0,0 +1,17 @@ +with { fetch = import ./nix/fetch.nix; }; +let + + # The (pinned) Nixpkgs where the original packages are sourced from + pkgs = import fetch.nixpkgs {}; + + # The list of packages to be installed + shellPackages = [ snack-exe ]; + + snack-exe = (import fetch.snack).snack-exe; + snack-lib = (import fetch.snack).snack-lib; +in + if pkgs.lib.inNixShell + then pkgs.mkShell + { buildInputs = shellPackages; + } + else snack-lib.inferHPackBuild ./package.yaml diff --git a/nix/fetch.nix b/nix/fetch.nix new file mode 100644 index 0000000..7da0c48 --- /dev/null +++ b/nix/fetch.nix @@ -0,0 +1,23 @@ +# A record, from name to path, of the third-party packages +let + versions = builtins.fromJSON (builtins.readFile ./versions.json); + 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; +in + builtins.mapAttrs (_: spec: + fetchTarball { + url = + with spec; + "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"; + sha256 = spec.sha256; + } + ) versions diff --git a/nix/versions.json b/nix/versions.json new file mode 100644 index 0000000..5a95232 --- /dev/null +++ b/nix/versions.json @@ -0,0 +1,16 @@ +{ + "nixpkgs": { + "owner": "NixOS", + "repo": "nixpkgs-channels", + "branch": "nixos-18.09", + "rev": "c2950341d038995bf46a7b72db961bb3d3e9ac12", + "sha256": "1adpvp20b3zwxchnvkag8d0m7qlnwxv3l9z1lg96kr5ffwk37prv" + }, + "snack": { + "owner": "nmattia", + "repo": "snack", + "branch": "master", + "rev": "9754f4120d28ce25888a608606deddef9f490654", + "sha256": "1a2gcap2z41vk3d7cqydj880lwcpxljd49w6srb3gplciipha6zv" + } +} diff --git a/package.yaml b/package.yaml new file mode 100644 index 0000000..c420a62 --- /dev/null +++ b/package.yaml @@ -0,0 +1,9 @@ +name: niv + +executable: + source-dirs: . # remove when https://github.com/nmattia/snack/pull/96 is merged + main: Main.hs + dependencies: + - directory + - filepath + - optparse-applicative