From 31583d7cb612a0d19a94fd1e03096b9da138b7d5 Mon Sep 17 00:00:00 2001
From: Michael Weiss <dev.primeos@gmail.com>
Date: Wed, 1 Aug 2018 18:18:19 +0200
Subject: [PATCH 1/4] androidStudioPackages: Refactor the code

---
 .../editors/android-studio/common.nix         | 19 ++++++++-
 .../editors/android-studio/default.nix        | 41 +++++--------------
 2 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/pkgs/applications/editors/android-studio/common.nix b/pkgs/applications/editors/android-studio/common.nix
index 8a536bad9c4..698602085fe 100644
--- a/pkgs/applications/editors/android-studio/common.nix
+++ b/pkgs/applications/editors/android-studio/common.nix
@@ -1,4 +1,5 @@
-{ pname, version, build, sha256Hash, meta }:
+{ channel, pname, version, build, sha256Hash }:
+
 { bash
 , buildFHSUserEnv
 , coreutils
@@ -128,4 +129,18 @@ in
       #!${bash}/bin/bash
       ${fhsEnv}/bin/${pname}-fhs-env ${androidStudio}/bin/studio.sh
     '';
-  } // { inherit meta; }
+  } // {
+    meta = with stdenv.lib; {
+      description = "The Official IDE for Android (${channel} channel)";
+      longDescription = ''
+        Android Studio is the official IDE for Android app development, based on
+        IntelliJ IDEA.
+      '';
+      homepage = if channel == "stable"
+        then https://developer.android.com/studio/index.html
+        else https://developer.android.com/studio/preview/index.html;
+      license = licenses.asl20;
+      platforms = [ "x86_64-linux" ];
+      maintainers = with maintainers; [ primeos ];
+    };
+  }
diff --git a/pkgs/applications/editors/android-studio/default.nix b/pkgs/applications/editors/android-studio/default.nix
index ab76166a8f9..73f2724f0e1 100644
--- a/pkgs/applications/editors/android-studio/default.nix
+++ b/pkgs/applications/editors/android-studio/default.nix
@@ -12,7 +12,12 @@ let
     build = "173.4819257";
     sha256Hash = "196yaswbxh2nd83gimjxr8ggr5xkdxq7n3xlh6ax73v59pj4hryq";
   };
-  latestVersion = {
+  betaVersion = {
+    version = "3.2.0.22"; # "Android Studio 3.2 Beta 5"
+    build = "181.4913314";
+    sha256Hash = "016nyn1pqviy089hg0dq7m4cqb39fdxdcy4zknkaq7dmgv1dj6x9";
+  };
+  latestVersion = { # canary & dev
     version = "3.3.0.2"; # "Android Studio 3.3 Canary 3"
     build = "181.4884283";
     sha256Hash = "0r93yzw87cgzz60p60gknij5vaqmv1a1kyd4cr9gx8cbxw46lhwh";
@@ -24,48 +29,24 @@ in rec {
   # Attributes are named by the corresponding release channels
 
   stable = mkStudio (stableVersion // {
+    channel = "stable";
     pname = "android-studio";
     #pname = "android-studio-stable"; # TODO: Rename and provide symlink
-
-    meta = with stdenv.lib; {
-      description = "The Official IDE for Android (stable channel)";
-      longDescription = ''
-        Android Studio is the official IDE for Android app development, based on
-        IntelliJ IDEA.
-      '';
-      homepage = https://developer.android.com/studio/index.html;
-      license = licenses.asl20;
-      platforms = [ "x86_64-linux" ];
-      maintainers = with maintainers; [ primeos ];
-    };
   });
 
-  beta = mkStudio (latestVersion // {
+  beta = mkStudio (betaVersion // {
+    channel = "beta";
     pname = "android-studio-preview";
     #pname = "android-studio-beta"; # TODO: Rename and provide symlink
-    version = "3.2.0.22"; # "Android Studio 3.2 Beta 5"
-    build = "181.4913314";
-    sha256Hash = "016nyn1pqviy089hg0dq7m4cqb39fdxdcy4zknkaq7dmgv1dj6x9";
-
-    meta = stable.meta // {
-      description = "The Official IDE for Android (beta channel)";
-      homepage = https://developer.android.com/studio/preview/index.html;
-    };
   });
 
   dev = mkStudio (latestVersion // {
+    channel = "dev";
     pname = "android-studio-dev";
-
-    meta = beta.meta // {
-      description = "The Official IDE for Android (dev channel)";
-    };
   });
 
   canary = mkStudio (latestVersion // {
+    channel = "canary";
     pname = "android-studio-canary";
-
-    meta = beta.meta // {
-      description = "The Official IDE for Android (canary channel)";
-    };
   });
 }

From d8b44edd8fda0d03a71d7f3482ec5df11f1c3a2d Mon Sep 17 00:00:00 2001
From: Michael Weiss <dev.primeos@gmail.com>
Date: Wed, 1 Aug 2018 18:40:07 +0200
Subject: [PATCH 2/4] androidStudioPackages: Use more intuitive names for the
 derivations

---
 pkgs/applications/editors/android-studio/common.nix  | 10 ++++++----
 pkgs/applications/editors/android-studio/default.nix |  4 +---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/pkgs/applications/editors/android-studio/common.nix b/pkgs/applications/editors/android-studio/common.nix
index 698602085fe..cd031ce5ac8 100644
--- a/pkgs/applications/editors/android-studio/common.nix
+++ b/pkgs/applications/editors/android-studio/common.nix
@@ -37,8 +37,9 @@
 }:
 
 let
+  drvName = "android-studio-${channel}-${version}";
   androidStudio = stdenv.mkDerivation {
-    name = "${pname}-${version}";
+    name = drvName;
 
     src = fetchurl {
       url = "https://dl.google.com/dl/android/studio/ide-zips/${version}/android-studio-ide-${build}-linux.zip";
@@ -116,18 +117,19 @@ let
   # (e.g. `mksdcard`) have `/lib/ld-linux.so.2` set as the interpreter. An FHS
   # environment is used as a work around for that.
   fhsEnv = buildFHSUserEnv {
-    name = "${pname}-fhs-env";
+    name = "${drvName}-fhs-env";
     multiPkgs = pkgs: [ pkgs.ncurses5 ];
   };
 
 in
   writeTextFile {
-    name = "${pname}-${version}";
+    name = "${drvName}-wrapper";
+    # TODO: Rename preview -> beta (and add -stable suffix?):
     destination = "/bin/${pname}";
     executable = true;
     text = ''
       #!${bash}/bin/bash
-      ${fhsEnv}/bin/${pname}-fhs-env ${androidStudio}/bin/studio.sh
+      ${fhsEnv}/bin/${drvName}-fhs-env ${androidStudio}/bin/studio.sh
     '';
   } // {
     meta = with stdenv.lib; {
diff --git a/pkgs/applications/editors/android-studio/default.nix b/pkgs/applications/editors/android-studio/default.nix
index 73f2724f0e1..b60687c50ba 100644
--- a/pkgs/applications/editors/android-studio/default.nix
+++ b/pkgs/applications/editors/android-studio/default.nix
@@ -26,18 +26,16 @@ in rec {
   # Old alias
   preview = beta;
 
-  # Attributes are named by the corresponding release channels
+  # Attributes are named by their corresponding release channels
 
   stable = mkStudio (stableVersion // {
     channel = "stable";
     pname = "android-studio";
-    #pname = "android-studio-stable"; # TODO: Rename and provide symlink
   });
 
   beta = mkStudio (betaVersion // {
     channel = "beta";
     pname = "android-studio-preview";
-    #pname = "android-studio-beta"; # TODO: Rename and provide symlink
   });
 
   dev = mkStudio (latestVersion // {

From 88bfbf6c7d2308b59f1ca69169c8c9853815ae6b Mon Sep 17 00:00:00 2001
From: Michael Weiss <dev.primeos@gmail.com>
Date: Wed, 1 Aug 2018 19:05:01 +0200
Subject: [PATCH 3/4] android-studio-preview: Print a deprecation warning

Hope this is ok... :)
---
 .../editors/android-studio/common.nix            | 16 ++++++++++++++--
 .../editors/android-studio/default.nix           |  8 ++++++--
 pkgs/top-level/all-packages.nix                  |  2 +-
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/pkgs/applications/editors/android-studio/common.nix b/pkgs/applications/editors/android-studio/common.nix
index cd031ce5ac8..c980999dc6b 100644
--- a/pkgs/applications/editors/android-studio/common.nix
+++ b/pkgs/applications/editors/android-studio/common.nix
@@ -1,4 +1,4 @@
-{ channel, pname, version, build, sha256Hash }:
+{ channel, pname, version, build, sha256Hash, deprecated ? false }:
 
 { bash
 , buildFHSUserEnv
@@ -37,6 +37,18 @@
 }:
 
 let
+  # TODO: This is a bit stupid to be honest...
+  # The problem is that we have to make sure this is only executed if the
+  # derivation is actually build to avoid always printing this warning (e.g.
+  # "nix-env -qaP"). Since this will always evaluate to "" it won't actually
+  # change the derivation (only generate a side-effect) but we have to make
+  # sure this expression is evaluated lazily!
+  printDeprecationWarning = if deprecated then (builtins.trace ''
+    android-studio-preview and androidStudioPackages.preview are old aliases
+    and will be dropped at some point, please use androidStudioPackages.beta
+    instead (corresponds to the correct channel name).''
+    "")
+    else "";
   drvName = "android-studio-${channel}-${version}";
   androidStudio = stdenv.mkDerivation {
     name = drvName;
@@ -130,7 +142,7 @@ in
     text = ''
       #!${bash}/bin/bash
       ${fhsEnv}/bin/${drvName}-fhs-env ${androidStudio}/bin/studio.sh
-    '';
+    '' + printDeprecationWarning;
   } // {
     meta = with stdenv.lib; {
       description = "The Official IDE for Android (${channel} channel)";
diff --git a/pkgs/applications/editors/android-studio/default.nix b/pkgs/applications/editors/android-studio/default.nix
index b60687c50ba..af58611bddf 100644
--- a/pkgs/applications/editors/android-studio/default.nix
+++ b/pkgs/applications/editors/android-studio/default.nix
@@ -23,8 +23,12 @@ let
     sha256Hash = "0r93yzw87cgzz60p60gknij5vaqmv1a1kyd4cr9gx8cbxw46lhwh";
   };
 in rec {
-  # Old alias
-  preview = beta;
+  # TODO: Drop old alias after 18.09
+  preview = mkStudio (betaVersion // {
+    channel = "beta";
+    pname = "android-studio-preview";
+    deprecated = true;
+  });
 
   # Attributes are named by their corresponding release channels
 
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 8ce802c2fb7..940f6f60be4 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -15170,7 +15170,7 @@ with pkgs;
 
   androidStudioPackages = callPackage ../applications/editors/android-studio { };
   android-studio = androidStudioPackages.stable;
-  android-studio-preview = androidStudioPackages.beta;
+  android-studio-preview = androidStudioPackages.preview; # TODO: Drop old alias after 18.09
 
   antfs-cli = callPackage ../applications/misc/antfs-cli {};
 

From 8caeec1ba78df3c0dbf3e8a8e9b1127073654799 Mon Sep 17 00:00:00 2001
From: Michael Weiss <dev.primeos@gmail.com>
Date: Wed, 1 Aug 2018 19:44:32 +0200
Subject: [PATCH 4/4] androidStudioPackages: Use recurseIntoAttrs

IMO it makes sense that the other channels show up in nix-env as well.
---
 pkgs/top-level/all-packages.nix | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 940f6f60be4..39eb6a3bbb2 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -15168,7 +15168,8 @@ with pkgs;
 
   amsn = callPackage ../applications/networking/instant-messengers/amsn { };
 
-  androidStudioPackages = callPackage ../applications/editors/android-studio { };
+  androidStudioPackages = recurseIntoAttrs
+    (callPackage ../applications/editors/android-studio { });
   android-studio = androidStudioPackages.stable;
   android-studio-preview = androidStudioPackages.preview; # TODO: Drop old alias after 18.09