Skip to content

Commit

Permalink
Add flake template
Browse files Browse the repository at this point in the history
  • Loading branch information
jonringer committed Jul 7, 2021
1 parent 4108dcc commit e3b5bd6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ $ nix-template config nixpkgs-root ~/nixpkgs
.arg(
Arg::from_usage("[PATH] 'directory or file to be written. In the case of a directory, a default.nix will be created. When used with --nixpkgs, it will be appended to nixpkgs-root to determine path location.'")
.default_value("default.nix")
.default_value_if("TEMPLATE", Some("mkshell"), "shell.nix"),
.default_value_if("TEMPLATE", Some("mkshell"), "shell.nix")
.default_value_if("TEMPLATE", Some("flake"), "flake.nix"),
)
.arg(Arg::from_usage(
"-l,--license [license] 'Set license'",
Expand Down
40 changes: 40 additions & 0 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn derivation_helper(template: &Template) -> (String, String) {
Template::qt => ("mkDerivation", "mkDerivation", None),
Template::go => ("buildGoModule", "buildGoModule", None),
Template::rust => ("rustPlatform", "rustPlatform.buildRustPackage", None),
Template::flake => ("", "", None) // flakes aren't a normal expression
};

match documentation_key {
Expand Down Expand Up @@ -88,6 +89,45 @@ fn meta() -> &'static str {

pub fn generate_expression(info: &ExpressionInfo) -> String {
match &info.template {
Template::flake => r#"{
description = "CHANGEME";
inputs = {
utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
};
outputs = { self, nixpkgs, utils }:
let
# put devShell and any other required packages into local overlay
localOverlay = import ./nix/overlay.nix;
pkgsForSystem = system: import nixpkgs {
# if you have additional overlays, you may add them here
overlays = [
localOverlay # this should expose devShell
];
inherit system;
};
# https://github.com/numtide/flake-utils#usage for more examples
in utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" ] (system: rec {
legacyPackages = pkgsForSystem system;
packages = utils.lib.flattenTree {
inherit (pkgs) devShell myPkg;
};
defaultPackage = packages.myPkg;
apps.<mypkg> = utils.lib.mkApp { drv = packages.myPkg; }; # use as `nix run <mypkg>`
hydraJobs = { inherit (legacyPackages) myPkg; };
checks = { inherit (legacyPackages) myPkg; }; # items to be ran as part of `nix flake check`
}) // {
# non-system suffixed items should go here
overlay = localOverlay;
overlays = []; # list or attr set of overlays
nixosModule = { config }: { options = {}; config = {};}; # export single module
nixosModules = {}; # attr set or list
nixosConfigurations.hostname = { config, pkgs }: {};
};
}"#.to_string(),
Template::mkshell => "with import <nixpkgs> { };
mkShell rec {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ fn main() {

// ensure directory to file exists
if let Some(p) = path.parent() {
if !path.exists() {
// TODO: better way to determine that file will be written PWD
if p.to_str() != Some("") && !p.exists() {
println!("Creating directory: {}", p.display());
std::fs::create_dir_all(p)
.unwrap_or_else(|_| panic!("Was unable to create directory {}", p.display()));
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ arg_enum! {
#[allow(non_camel_case_types)]
#[derive(Debug,PartialEq)]
pub enum Template {
flake,
stdenv,
python,
mkshell,
Expand Down

0 comments on commit e3b5bd6

Please sign in to comment.