about summary refs log blame commit diff
path: root/scripts/nix-push.in
blob: c1624d8355be236bf7d534c83f42846959dea77e (plain) (tree)
1
2
3
4
5
6
7
8
9

                   






                          
                                        



                                                         

                      
 

          
                        
 
                            

                                                                 
                     

                                                 
 
                                                                                                
                     
              
                                   
                        


                
                                                                     


                               

                                                  
 






                                                                

     
 

              
 
 
                                                         
         

                                                                            

                

                     
 
           
 
 
                               
                                      

                                                  
 
             
                                                                      


                       
                         
 
































                                                            
                                           


                                    






                                                                                            






                         

                                             
                                      


                                                                                                        
#! /usr/bin/perl -w

use strict;
use POSIX qw(tmpnam);

my $tmpdir;
do { $tmpdir = tmpnam(); }
until mkdir $tmpdir, 0777;

my $nixfile = "$tmpdir/create-nars.nix";
my $manifest = "$tmpdir/MANIFEST";

END { unlink $manifest; unlink $fixfile; rmdir $tmpdir; }

open NIX, ">$nixfile";
print NIX "[";

my @paths;

foreach my $id (@ARGV) {

    die unless $id =~ /^\//;

    # Get all paths referenced by the normalisation of the given 
    # Nix expression.
    system "nix-store --realise $id > /dev/null";
    die if ($?);

    open PATHS, "nix-store --query --requisites --include-successors $id 2> /dev/null |" or die;
    while (<PATHS>) {
        chomp;
        die "bad: $_" unless /^\//;
        push @paths, $_;
    }
    close PATHS;

    # For each path, create a Nix expression that turns the path into
    # a Nix archive.
    foreach my $path (@paths) {

        die unless ($path =~ /\/[0-9a-z]{32}.*$/);
        print "$path\n";

        # Construct a Nix expression that creates a Nix archive.
        my $nixexpr = 
            "((import @datadir@/nix/corepkgs/nar/nar.nix) " .
            # !!! $path should be represented as a closure
            "{path = \"$path\"; system = \"@host@\"}) ";
        
        print NIX $nixexpr;
    }
}

print NIX "]";
close NIX;


# Instantiate a store expression from the Nix expression.
my @nids;
print STDERR "instantiating Nix expression...\n";
open NIDS, "nix-instantiate $nixfile |" or die "cannot run nix-instantiate";
while (<NIDS>) {
    chomp;
    die unless /^\//;
    push @nids, $_;
}
close NIDS;


# Realise the store expression.
print STDERR "creating archives...\n";
system "nix-store --realise -v @nids > /dev/null";
if ($?) { die "`nix --realise' failed"; }

my @narpaths;
open NIDS, "nix-store --query --list @nids |" or die "cannot run nix";
while (<NIDS>) {
    chomp;
    die unless (/^\//);
    push @narpaths, "$_";
}
close NIDS;

    
# Create the manifest.
print STDERR "creating manifest...\n";

open MANIFEST, ">$manifest";

my @pushlist;
push @pushlist, $manifest;

for (my $n = 0; $n < scalar @paths; $n++) {
    my $storepath = $paths[$n];
    my $nardir = $narpaths[$n];
    
    $storepath =~ /\/([^\/]*)$/;
    my $basename = $1;
    defined $basename or die;

    my $narname = "$basename.nar.bz2";

    my $narfile = "$nardir/$narname";
    (-f $narfile) or die "narfile for $storepath not found";
    push @pushlist, $narfile;

    open MD5, "$nardir/md5" or die "cannot open hash";
    my $hash = <MD5>;
    chomp $hash;
    $hash =~ /^[0-9a-z]{32}$/ or die "invalid hash";
    close MD5;

    print MANIFEST "{\n";
    print MANIFEST "  StorePath: $storepath\n";
    print MANIFEST "  NarName: $narname\n";
    print MANIFEST "  MD5: $hash\n";

    if ($storepath =~ /\.nix$/) {
        open PREDS, "nix-store --query --predecessors $storepath |" or die "cannot run nix";
        while (<PREDS>) {
            chomp;
            die unless (/^\//);
            print MANIFEST "  SuccOf: $_\n";
        }
        close PREDS;
    }

    print MANIFEST "}\n";
}

close MANIFEST;


# Push the prebuilts to the server. !!! FIXME
print STDERR "pushing to server...\n";
if (scalar @pushlist > 0) {
    system "rsync -av -e ssh @pushlist eelco\@losser.st-lab.cs.uu.nl:/home/eelco/public_html/nix-dist/";
}